Configure sails server to work with html5Mode?

I have an angular -sails application where I want to remove a hash from urls

I added this to my client app.config

$locationProvider.html5Mode(true);

and routing works until I refresh the page.

transition to http://localhost:1337/aboutworks. I update and get

{
  "status": 404
}

from the sails server. How to configure sails to work with angularjs html5Mode?

+4
source share
3 answers

HTML5Mode, , root ( index.html). "Web.com/index.html". , , Sails Angular, . , -, Angular, index.html AJAX.

, "", , , Angular , , . , Angular X-Requested-With = XMLHttpRequest

module.exports = function(req, res, next) {

  // Checks if this is an ajax request 
  // If True then skip this and return requested content
  // If False then return the index (or root) page  
  if (if !req.isAjax) {
    return res.view("/index");
  }

  return next();
};
+3

, SO, . SailsJS

, , angular.

+1

, , . , URL, /admin . :

routes.js:

'GET /admin/*': 'AdminController.index'

AdminController.js:

var path = require('path');
var fs = require('fs');

module.exports = {
    index: function(req, res) {
        var requestedPath = req.url;
        var resolvedPath = path.resolve(requestedPath);
        if (resolvedPath.indexOf('/admin') !== 0) {
            res.send(403);
        } else {
            var publicRoot = sails.config.paths.public;
            var fullPath = path.join(publicRoot, resolvedPath);
            fs.exists(fullPath, function(exists) {
                if (exists) {
                    res.sendfile(resolvedPath, { root: publicRoot });
                } else {
                    res.sendfile('/admin/index.html', { root: publicRoot });
                }
            });
        }
    }
};

(, .js), ; index.html. .

0

Source: https://habr.com/ru/post/1547629/


All Articles