Sails js route static html

扶醉桌前 提交于 2019-12-07 00:07:34
hawkcurry

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!