5

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?

BBaysinger
  • 6,614
  • 13
  • 63
  • 132
user2662882
  • 165
  • 1
  • 2
  • 9

1 Answers1

7

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

jitender
  • 10,238
  • 1
  • 18
  • 44
  • Thanks for this info. One thing though, how would I go about making this run in production, and build from ng build? – BBaysinger May 23 '18 at 16:45
  • it depends upon the tech stack you are using for the backend.you can rander replace the index view with angular index html or you can also use nginx an proxy server that can redirect your api requests to the port you want. – jitender May 23 '18 at 17:21