简体   繁体   中英

Sails.js Single Page Application (SPA) — redirect all missing/unused routes to a single controller action

I'm developing a single page application (SPA) using Sails.js as a backend. All I want is to redirect all routes to a single controller action.

However, when I do the following:

// config/routes.js
module.exports.routes = {
  'GET *': 'MainController.application'
};

All requests are getting redirected to my application route, even for static files like CSS/JavaScript, etc. Is there an easy way to fallback to my application route when there is no other means to handle it?

I want:

  1. All static files to be served directly (JS, CSS, HTML partials)
  2. All specific routes to be handled as is
  3. In other case redirect to a single entry-point controller

After thorough re-reading of the docs I've found a skipAssets route parameter.

Here's my new configuration:

// config/routes.js
module.exports.routes = {
  'GET *': {
    controller: 'MainController',
    action: 'application',
    skipAssets: true
  }
};

Looks like it's working as required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM