Which npm webserver can I use when rewriting urls

I am doing a SPA with AngularJS and need a local webserver that supports url rewriting. All queries such as /home

, /profile/1

, /products?id=12

should return the index.html in the root folder. I've tried http-server , local-web-server , but they don't include redirection functionality. I think superstatic should work but for some reason it returns 404 error

+3


source to share


2 answers


Try expressjs

You can use something like



 app.route('/*')
    .get(function(req, res) {
      res.sendFile(path.resolve(app.get('appPath') + '/index.html'));
    });

      

You can check this generator

+1


source


Express allows "*" and also allows regular expressions.

Most of the time in production you will probably need to serve your webapp from nginx or whatever, and the Express api reverse proxy through nginx and bundle them together. nginx will cache much better and the like. (and will probably be much more secure)



However ... Use gulpjs (I think you can grunt too) and then use http://www.browsersync.io/ and https://github.com/shakyShane/browser-sync-spa to reload your interface and it will help you figure it out if you are just frustrated with routing.

0


source







All Articles