How to run Angular 2 client app and Node server ap

2020-04-11 08:32发布

问题:

I have built an app in Angular 2 to fetch data from a DB, and used node/express to fetch data from the server and serve it to the Angular client. Currently both of them are running in different local hosts. How do I combine them into a single project and run it on the same host?

回答1:

Suppose your express site is running on localhost:3500 and all api call begins with /api

create proxy.conf.json with the content

{
  "/api": {
    "target": "http://localhost:3500",
    "secure": false
  }
}

then edit package.json and change "start": "ng serve", to "start": "ng serve --proxy-config proxy.conf.json"

now run the project using command npm start.Note:- ng serve will not work

For more information find documatation here