I want to completely separate the client and server sides in my sails js app.
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?
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'
],
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.
The 'www' middleware
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;
})(),
By default this module will send "index.html" files in response to a request on a directory.
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.
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?
You can place your Html
files in assets folder. Sails will serve the html file for requested url.
Say, you place a html file about.html
in assets
folder, Sails will serve it when a url <your-domain>/about.html
is requested.
来源:https://stackoverflow.com/questions/40462684/sails-js-route-static-html