Making Angular routes work with Express routes

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

提交回复
热议问题