Reputation: 101
I have a node server running at port 8000
app.get('/historical/:days' ,(req,res,next){..})
How do I make a a request from angular(port 4200) from the browser to the node server? I do the following
makeRequest(): void {
this.loading = true;
this.http.get('http://localhost:8000/historical/:day')
.subscribe(data => {
this.data = data;
console.log(this.data)
this.loading = false; });
}
at the html side of angular:
<pre>
<div>
{{makeRequest()}}
</div>
</pre>
When I type http://localhost:4200/historical/1
, (where 1 represents historical data of the previous day)
nothing happens
What to do?
Upvotes: 0
Views: 263
Reputation: 13396
For one I would not call the makeRequest
function from your html. Angular is going to fire this method every time the change detection cycle runs (alot of api requests). Instead I would include it in your ngOnInit lifecycle hook
ngOnInit(){
this.makeRequest();
}
Since your node server is running on localhost:8000, then
this.http.get('http://localhost:8000/historical/1').subscribe(...);
should work. If you want to hit localhost:4200 for an api request, you are going to need to proxy your api calls from localhost:4200 to localhost:8000. Angular CLI proxy docs
Upvotes: 1