Making Angular routes work with Express routes

后端 未结 5 983
无人共我
无人共我 2020-12-31 08:02

I\'ve got in impasse with setting Angular routes to work with Express.

I tried to do like here Express 4, NodeJS, AngularJS routing but that did not work. The stati

5条回答
  •  借酒劲吻你
    2020-12-31 08:20

    While the solution suggested by @kirgol of using base href= works fine, there is an alternative solution which might interest someone looking for simple route serve. In this solution we will not using ExpressJS routing

    Simply use this

    app.use(express.static(__dirname + '/app'));
    

    instead of this

    app.all('/*', function(req, res, next) {
        // Just send the index.html for other files to support HTML5Mode
        res.sendFile('index.html', { root: __dirname });
    });
    

    This way expressJS will automatically serve the index.html present at that static location & will handle the routing automatically. It will behave just like a simple http server. The moment you handle the routing using app.all/ app.get - you will have to set base href to tell $locationProvider of location from which it can match routes

提交回复
热议问题