2

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:

enter image description here

The node defined accessing route: enter image description here

The configuration of the node server:

enter image description here

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. enter image description here

Someone knows what can be happening? Thanks for reading!

1 Answer 1

2

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 } });
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this worked! It's not any other way for sending it without been the parameter setted in the url? @eol
Not if you want it as a path-parameter. If you change it to query to can pass a query-object. I've updated my answer :)
Thanks, this helped to understand routing parameters. @eol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.