Reputation: 453
I'm developing an easy website using Angular and nodeJS. I'm trying to send one parameter "email" from an Angular service when accessing a route defined in node server. The Angular service function:
The node defined accessing route:
The configuration of the node server:
I tried using bodyParser and using req.body for getting the email parameter sent from the frontend, but it seems to be lost.
It's getting an empty dictionary when req.params or req.body are printed.
Someone knows what can be happening? Thanks for reading!
Upvotes: 2
Views: 503
Reputation: 24555
From the looks of it you want to read the path-param
from the request url (see route-params under https://expressjs.com/en/guide/routing.html).
On the nodejs side, you need to change your handler to:
router.get('/profile/:email', ...) => {
const email = req.params.email;
});
On the client-side, you need to correctly add the email path-segment. One way to do this is:
this.http.get<any>(`${this.URL}/profile/` + encodeURIComponent(email));
Instead of path
-parameters you can also do this is using query
-parameters. You'd need the following changes:
nodejs:
router.get('/profile', ...) => {
const email = req.query.email;
});
angular:
this.http.get<any>(`${this.URL}/profile`, { params: { email } });
Upvotes: 2