-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Follow the NG CLI wiki page (proxy to backend, I am trying to configure proxy to the backend API.
The backend is a nodejs/express application, served at port 3000. The frontend is NG 2 application generated by Angular CLI.
I added a proxy.conf.json in the root folder of frontend application.
{
"/api/*": {
"target": "http://localhost:3000",
"changeOrigin": true,
"secure": false,
"logLevel": "debug"
}
}
and changed the start
command in package.json. I also tried the simplest config in the Wiki page(proxy to backend, it does not work).
"start": "ng serve --port 4300 --proxy-config proxy.conf.json",
When I run the application, I got error messages from cmd console.
[HPM] GET /api/upload/uploads-1490782788752.png -> http://localhost:3000
[HPM] Error occurred while trying to proxy request /api/upload/uploads-1490782788752.png from localhost:4300 to http://localhost:3000 (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)
The image should be accessed via http://localhost:4300/api/upload/uploads-1490782788752.png, but failed, and it is existed. When access http://localhost:3000/api/upload/uploads-1490782788752.png, it displays the image directly.
When I accessed http://localhost:4300/api/upload/uploads-1490782788752.png, in Chrome browser. It displays a message:
Error occured while trying to proxy to: localhost:4300/api/upload/uploads-1490782788752.png
My local development environment:
- Node
node -v
v7.7.4
- npm
npm -v
4.1.2
- NG 1.0.0
ng -v
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
@angular/cli: 1.0.0
node: 7.7.4
os: win32 x64
@angular/common: 2.4.10
@angular/compiler: 2.4.10
@angular/core: 2.4.10
@angular/forms: 2.4.10
@angular/http: 2.4.10
@angular/platform-browser: 2.4.10
@angular/platform-browser-dynamic: 2.4.10
@angular/router: 3.4.10
@angular/cli: 1.0.0
@angular/compiler-cli: 2.4.10
- OS
Windows 10 64 bit, 1607
I have tried to set target in the proxy.conf.json using 127.0.0.1, dev.local(virtual hosts configured in hosts
file) instead of localhost, all failed under Windows 10.
But the the internal network ip(192.168.0.110) works.
{
"/api": {
"target": "http://192.168.0.110:3000",
"changeOrigin": true,
"secure": false,
"logLevel": "debug"
}
}
I think this is Windows specific bug.
BTW,1. I also used proxy on localhost in before projects, it works. eg. https://github.com/hantsy/angularjs-springmvc-sample-boot/blob/master/gulpfile.js, proxy configured via proxy-middleware.
2. I have tried to use ng eject
to export webpack.config and set proxy in it, and got the same errors.