您需要的是至少需要支持動態內容的http-server。而你的http服務器僅支持靜態內容。
並在評論中詢問你應該使用哪個服務器。有數千個支持動態內容的網絡服務器。但你正在使用sinc http-server
我認爲你只是想爲本地開發小型服務器。
不幸的是,我找不到任何支持您的需求的服務器,而無需修改它們的代碼。所以我建議你在npm上的庫上創建你自己的服務器庫。
這是和示例服務器使用live-server。
var liveServer = require("live-server");
var fs = require("fs")
var root = process.argv[2] || "."
var port = process.argv[3] || 8000
var replaceTextMiddleWare = function(req, res, next){
var file = process.argv[4]
var find = process.argv[5]
var replace = process.argv[6]
if(file && find){
if(req.url === file) {
fs.readFile(root + file, "utf-8", function(e, content){
res.end(content.replace(find, replace))
})
return;
}
}
next();
}
var params = {
port: port, // Set the server port. Defaults to 8080.
host: "0.0.0.0", // Set the address to bind to. Defaults to 0.0.0.0 or process.env.IP.
root: root, // Set root directory that's being server. Defaults to cwd.
open: false, // When false, it won't load your browser by default.
ignore: 'scss,my/templates', // comma-separated string for paths to ignore
file: "index.html", // When set, serve this file for every 404 (useful for single-page applications)
wait: 1000, // Waits for all changes, before reloading. Defaults to 0 sec.
mount: [['/components', './node_modules']], // Mount a directory to a route.
logLevel: 2, // 0 = errors only, 1 = some, 2 = lots
middleware: [ replaceTextMiddleWare ] // Takes an array of Connect-compatible middleware that are injected into the server middleware stack
};
liveServer.start(params);
然後你就可以通過
nodejs myserver.js /mydocument/myproject/ 8000 config.js "http://mybackend.com:3026" "http://mydevserver.com:80"
運行服務器的命令接受參數:
- 路徑提供內容
- 端口
- 文件名
- 文本找到
- 要替換的文本
此服務器僅支持一個帶簡單查找/替換的動態文件。 從這一點來說,我猜你可以修改中間件來做你想做的任何事情。
您需要的是至少需要支持動態內容的http-server。而你的'http-server'只支持靜態內容。 – Hereblur
Hereblur,你能告訴我如何?我應該使用哪個服務器? –
我認爲這個配置應該是你的構建設置的一部分,而不是服務器設置。 – gvmani