如果你看看 sails 使用的中间件
配置/http.js
order: [
'startRequestTimer',
'cookieParser',
'session',
'myRequestLogger',
'bodyParser',
'handleBodyParserError',
'compress',
'methodOverride',
'poweredBy',
'$custom',
'router',
'www',
'favicon',
'404',
'500'
],
我们可以看到它尝试将当前请求与router
中间件定义为config/routes.js
如果没有找到,则尝试在www
中间件。如果两者都失败,则返回 404。
“www”中间件
The www
中间件简单地使用express的serve-static中间件。
www: (function() {
var flatFileMiddleware = require('serve-static')(sails.config.paths['public'], {
maxAge: sails.config.http.cache
});
return flatFileMiddleware;
})(),
默认情况下,该模块将发送“index.html”文件以响应
对目录的请求。
所以如果你想使用 sail 的默认中间件,那么你可以将你的 html 文件放入assets/index.html
, assets/about/index.html
, and assets/foo/index.html
将用于/
, /about
, and /foo
分别。
但是,如果您确实想对此进行控制,那么您可以替换 www 中间件并将其替换为您自己的中间件。阅读 sail 的文档中间件 http://sailsjs.org/documentation/concepts/middleware and 服务静态的文档 https://github.com/expressjs/serve-static以及。
您可以查看有关此主题的类似问题:有没有办法在没有扩展名的情况下从express提供静态html文件? https://stackoverflow.com/questions/16895047/any-way-to-serve-static-html-files-from-express-without-the-extension