Confusion about web-application ports

天涯浪子 提交于 2019-12-02 06:04:16

Trying to serve index.html as a static file won't work. Instead, try the following:

1. Serve your index.html from Sails

Just serve index.html as a combination of views/layout.ejs and views/home/index.ejs, which are mounted to the root / for default newly created Sails project.

2. Set up a catch-all route

In config/routes.js put something like this:

module.exports.routes = {
  '/': {
    view: 'home/index'
  },

  '/:unknownRoute': {
    view: 'home/index'
  }
}

This way you'll be able, for example, to use simple one-level pushstate routing within your SPA: routes like /products or /news will still give you your index.html (if you are using something more complex though, you may want to play a little bit more with your Sails routes).

3. Serve your API with a prefix

In your config/controllers.js put, for example:

module.exports.controllers = {
  ...

  prefix: '/api',

  ...
}

This will let you serve your API with a prefix and have both /api/products (JSON API) and /products (your SPA) routes available.

4. Use any port you want

You can change the default port via config/local.js, even to 80 (if you don't have anything else running on 80, of course).

In production though, it would probably be a better idea to just proxy to default Sails' or any other port with Nginx, for example.

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