你想要的是叫做“静态网页服务器”的东西。有很多方法可以实现这个目标。
它在静态网页服务器中有列出。
一个简单的方法:将下面的脚本保存为static_server.js。
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs")
port = process.argv[2] || 8888;
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname
, filename = path.join(process.cwd(), uri);
path.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}
response.writeHead(200);
response.write(file, "binary");
response.end();
});
});
}).listen(parseInt(port, 10));
console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown");
将你的index.html放在同一个目录下,然后运行。
node static_server.js
php,python或纯html? - Danhtml和css。将来可能会想要添加NodeJS,但那时我会有一个不同的设置。 - etsnymanfile://地址而不是http://地址存在严重的限制。一些链接和小的Javascript代码片段根本无法正常工作。 - etsnymanhttp://而不是file://,就需要一个网络服务器。Apache 是最常见的,还有其他的,如nginx,你可以选择任何一个。我对Jekyll不太熟悉,但如果你有经验,请使用它。 - Panther