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)); });