Sails JS路由静态HTML [英] Sails js route static html
问题描述
我想在我的Sails js应用程序中将客户端和服务器端完全分开.
I want to completely separate the client and server sides in my sails js app.
如果我删除"/"路由,它将自动从资产"文件夹中提供一个名为index.html的文件. 我想从资产文件夹中提供另一个文件,该怎么办?
If I remove the '/' route, automatically it will serve a file named index.html from the 'assets' folder. I want to serve another file from the assets folder, how can I do it?
推荐答案
如果您了解Sail使用的中间件
If you look at the middlewares that sails uses
config/http.js
order: [
'startRequestTimer',
'cookieParser',
'session',
'myRequestLogger',
'bodyParser',
'handleBodyParserError',
'compress',
'methodOverride',
'poweredBy',
'$custom',
'router',
'www',
'favicon',
'404',
'500'
],
我们可以看到它尝试将当前请求与config/routes.js
中定义的router
中间件进行匹配,如果未找到,则尝试在www
中间件中提供一个静态文件.如果两者均失败,则返回404.
We can see that it tries to match the current request with the router
middleware as defined in config/routes.js
and if none is found, tries to serve the a static file in the www
middleware. If both fail, then a 404 is returned.
"www"中间件
www
中间件仅使用express的服务静态中间件.
The www
middleware simply uses express's serve-static middleware.
www: (function() {
var flatFileMiddleware = require('serve-static')(sails.config.paths['public'], {
maxAge: sails.config.http.cache
});
return flatFileMiddleware;
})(),
默认情况下,此模块将发送"index.html"文件以响应 在目录上请求.
By default this module will send "index.html" files in response to a request on a directory.
因此,如果要使用Sail的默认中间件,则可以将html文件放入assets/index.html
,assets/about/index.html
和assets/foo/index.html
中,这些文件将用于/
,/about
和/foo
分别.
So if you want to use sail's default middlewares, then you can put your html files into assets/index.html
, assets/about/index.html
, and assets/foo/index.html
which will be served for /
, /about
, and /foo
respectively.
但是,如果您真的想对此进行控制,则可以替换www中间件并将其替换为自己的中间件.在中间件和
However, if you really want control over this, then you can replace the www middleware and replace it with your own middleware. Read up on sail's documentation on middlewares and serve-static's documentation as well.
You can check out similar questions on this topic: Any way to serve static html files from express without the extension?
这篇关于Sails JS路由静态HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!