Felix Dombek
Felix Dombek

Reputation: 14372

Is there any way to serve PHP on the same port as an Angular 4 CLI site?

I'm developing a webapp (frontend) in Angular 4 which connects to an existing webservice which will later sit in the same domain. I develop the frontend in Visual Studio Code with npm start which serves the site at localhost:4200. My PHP backend currently runs in XAMPP on port 80.

As no two servers can listen on the same port, is there any way I can serve these two things under the same port? Can I configure the Angular CLI to serve the PHP files as well?

Upvotes: 1

Views: 1626

Answers (1)

Casper Alant
Casper Alant

Reputation: 462

You can configure Angular CLI to proxy requests to another server.

Assuming all your backend requests are made to localhost/api/*: Add a file proxy.conf.json to your Angular project with the following contents:

{
"/api/*": {
    "target": "http://localhost",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true
    }
}

and serve the website using ng serve --proxy-config proxy.conf.json. Since you use npm start, you will have to change scripts->start in package.json.

Docs: https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md

Upvotes: 3

Related Questions