Making Angular routes work with Express routes

后端 未结 5 984
无人共我
无人共我 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:13

    I too faced a similar situation for routing through angular components using express, this is how I was able to resolve the routing issue:

    Setup for my Nodejs Project:

    • Build an Angular Project for production and placed in the public folder.
    • Created an app.js file for the express server with the following contents:

      var express = require('express'); var app = require('express')(); const port = 8090; app.use(express.static('public')); app.listen(port,()=>{console.log("App working at port : http://localhost:"+port)});

    Running this loaded the Root Component only, from where I had to manually route through different pages using routerLink. If I instead tried to give the child components directly using URL I got a Cannot GET/{URL} error.

    Fix:

    • I added the express-http-proxy package imported as
    • var proxy = require('express-http-proxy');
    • Then added in the following line after the app.use(express.static('public'));
    • app.use('/*',proxy('http://localhost:'+port+'/*'));

    This fixed my issue.Apparently there is a more standard fix Refer Link

    0 讨论(0)
  • 2020-12-31 08:19

    I just want to add that adding base href= helped me. I have my app statically served in express and browsing directly to routes in my angular app would cause lots of

    refused to execute script because its mime type ('text/html') is not executable and strict mime type checking is enabled

    errors to be throw. But adding base href fixed that and I can browse directly to my angular routes now.

    0 讨论(0)
  • 2020-12-31 08:20

    When you don't pass a path to express.use(), then it defaults to /. The proceeding rule you set for * is either redundant or may not even work.

    Also, since you're using html5mode, you need to explicitly set apart routes from resources so Express doesn't try to serve up your index.html file for all requests.

    Try this example from Angular UI-Router on for size:

    var express = require('express');
    var app = express();
    
    app.use('/js', express.static(__dirname + '/js'));
    app.use('/dist', express.static(__dirname + '/../dist'));
    app.use('/css', express.static(__dirname + '/css'));
    app.use('/partials', express.static(__dirname + '/partials'));
    
    app.all('/*', function(req, res, next) {
        // Just send the index.html for other files to support HTML5Mode
        res.sendFile('index.html', { root: __dirname });
    });
    
    app.listen(3006); //the port you want to use
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-31 08:33

    Thanks everybody for help. I resolved the question by inserting <base href="/" /> into head section of my Angular index.html. It worked for me, but as of now it's like magic and cargo coding, since I don't understand why it works :) Found solution here: AngularJS: how to enable $locationProvider.html5Mode with deeplinking

    0 讨论(0)
提交回复
热议问题