我想提供一个html文件而不指定它的扩展名.有没有办法在没有定义路线的情况下做到这一点?例如,而不是
/helloworld.html
Run Code Online (Sandbox Code Playgroud)
我想做的就是
/helloworld
Run Code Online (Sandbox Code Playgroud)
san*_*ari 49
你可以在express.static方法中使用扩展选项.
app.use(express.static(path.join(__dirname, 'public'),{index:false,extensions:['html']}));
Run Code Online (Sandbox Code Playgroud)
rob*_*lep 20
一个quick'n'dirty解决方案是附加.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));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8189 次 |
| 最近记录: |