我想提供一个html文件而不指定它的扩展名。有没有什么方法可以在不定义路由的情况下做到这一点?例如,它不是
/helloworld.html我想做的就是
/helloworld发布于 2013-06-03 19:17:07
一个快速的解决方案是将.html附加到没有句点的请求中,并且公共目录中存在对应的HTML文件:
var fs = require('fs');
var publicdir = __dirname + '/public';
app.use(function(req, res, next) {
if (req.path.indexOf('.') === -1) {
var file = publicdir + req.path + '.html';
fs.exists(file, function(exists) {
if (exists)
req.url += '.html';
next();
});
}
else
next();
});
app.use(express.static(publicdir));https://stackoverflow.com/questions/16895047
复制相似问题