Node.js:如何为所有url提供/index.html?
我正在使用Node.js,我想为所有url提供/index.html。
例如,我想为所有这些服务/index.html:
foo.com foo.com/index.html foo.com/bling foo.com/blah/index.html
如果可能的话,我宁愿不更改浏览器显示的URL。
这是可能的,如果是这样,最好的办法是什么?
例如,您可以使用express.js MVC npm模块
var express = require('express'); var app = express(); app.all('*', function(req, res){ res.sendfile("index.html"); }); app.listen(3000);
如果没有明确的规定,并且如果它是你的服务器必须完成的唯一任务,那么你可以在你的http
服务器实例maincallback中使用fs
模块,通过这种方式将一个readStream传递给响应:
http.createServer(function(request, response) { fs.createReadStream('index.html', { 'bufferSize': 4*1024 }).pipe(response); });