2013-05-11 30 views
1

我正在使用Node.js,我想为所有网址提供/index.html。Node.js:如何为所有网址提供/index.html?

例如,我想成为/index.html所有这些:

foo.com 
foo.com/index.html 
foo.com/bling 
foo.com/blah/index.html 

如果可能的话我宁愿不改变浏览器显示的URL。

这是可能的,如果是这样,最好的办法是什么?

回答

2

可以使用,例如,express.js MVC NPM模块

var express = require('express'); 
var app = express(); 

app.all('*', function(req, res){ 
    res.sendfile("index.html"); 
}); 

app.listen(3000); 
+2

看到你的答案太晚了,贴差不多了自己:)'res.sendfile(“的index.html”)'是充当最简单的方法'index.html'。 – robertklep 2013-05-11 18:10:41

+0

@robertklep谢谢你,我已更正 – zavg 2013-05-11 18:12:33

+0

感谢你们俩。我现在意识到一个明显的复杂因素......我想为/index.html中包含的文件提供服务:CSS,JavaScript和图像。我如何排除这些路径,即/ css,/ js,/ images等下的所有路径? – 2013-05-11 18:23:49

0

没有明确,如果这是你的服务器需要做的唯一任务,您可以使用fs模块在http服务器实例主要回调,管道中的readStream的回应是这样的:

http.createServer(function(request, response) { 
    fs.createReadStream('index.html', { 
     'bufferSize': 4*1024 
    }).pipe(response); 
}); 
相关问题