首页 » 前端 » NodeJS » 正文

node http server

发布者:站点默认
2022/11/26 浏览数(32) 分类:NodeJS node http server已关闭评论

package.json

{
  "scripts": {
    "serve": "node http-server.js",
  },
  "devDependencies": {
    "chalk": "^4.1.2",
    "connect": "^3.7.0",
    "http": "^0.0.1-security",
    "st": "^2.0.0"
  }
}

http-server.js

var connect = require("connect");
var http = require("http");
var st = require("st");       // https://npmmirror.com/package/st
var chalk = require("chalk"); // https://npmmirror.com/package/chalk
var port = 0;

var mount = st({
  path: ".",
  cache: false,
});
var app = connect().use(mount);
var server = http.createServer(app);
server.listen(0); // 自动获取一个可用端口
server.on("listening", function () {
  port = server.address().port;
  console.log("App running at: " + chalk.green("http://localhost:" + port));
});
点击返回顶部
  1. 留言
  2. 联系方式