How to setup Sails.js routes to support pushstate with a SPA on the frontend
If you're willing to use the development version of Sails from GitHub, you can use the skipRegex
route option to let your wildcard route ignore API requests, and the skipAssets
option to have it ignore asset URLs:
'/*' : {view: 'index', skipAssets: true, skipRegex: /^\/api\/.*$/}
Otherwise, you can create a controller to serve your view, and add the code to skip unintentionally-matched URLs in the action code:
// api/controllers/StaticController.jsmodule.exports = { index: function(req, res, next) { if (req.path.match(/\..*/g) || req.path.match(/^\/api\/.*$/)) { return next(); } return res.view('index'); }}
Then in /config/routes.js
:
'/*': 'StaticController.index'
......
Try updating your config/404.js
file.
Remove the following:
res.status(result.status); res.render(viewFilePath, function (err) { // If the view doesn't exist, or an error occured, send json if (err) { return res.json(result, result.status); } // Otherwise, serve the `views/404.*` page res.render(viewFilePath); });
Add this:
res.redirect("/");
Cheers