when I import HttpClient
to call my own written node.js API, there are some issues with the settings of the URL.
for example:
import { HttpClient, HttpHeaders } from '@angular/common/http';
export class myComponent implements OnInit {
constructor(
private http: HttpClient,
) {}
ngOnInit(): void {
this.http.get('http://127.0.0.1:3000/getData/theme').subscribe(data => {
});
});
}
//angular default port 4200,
//node.js default port 3000,
when I set this.http.get('/getData/theme')
the get
will be call http://127.0.0.1:4200
, this is wrong.
if I set this.http.get('http://127.0.0.1:3000/getData/theme')
for local development it works. but, when ng build
setting to actual server, it can't connect properly.
the console:
GET http://127.0.0.1:3000/getData/theme
net::ERR_CONNECTION_REFUSED
GET http://localhost:3000/structureData/themeData
net::ERR_CONNECTION_REFUSED
How can I set the correct URL to allow it to meet both online and local development status?
angular-cli server - how to proxy API requests to another server?
I set the package.json:
"start": "ng serve --proxy-config proxy.conf.json"
and
proxy.conf.json
{
"/api": {
"target": "http://localhost:3000",
"secure": false
}
}
it's not working:
this.http.get('/getData/theme')
GET http://localhost:4200/getData/theme 404 (Not Found)
or
this.http.get('/api/getData/theme')
GET http://localhost:4200/api/getData/theme 404 (Not Found)