Get information from a url using Node + express + ejs

Asked

Viewed 3,765 times

1

I set up a url in the following format: localhost:8080/forgetPassword/id/hash

I have this route rendering:

app.get('/forgetPassword', function(req, res) {
    res.render('pages/forgetPassword');
});

So I’d like to direct to the url localhost:8080/forgetPassord and grab the rest of the url id/hash where I’ll do the validation. However, I’m not finding a way to get this data.

1 answer

4


You can use params, that pass to properties of the req these values. The syntax is:

/:nomeDaVariavel that is to say: / + : + nome da variável

Forehead like this:

app.get('/forgetPassword',(req, res) => {
    res.render('pages/forgetPassword');
});

app.get('/forgetPassword/:id/:hash', (req, res) => {
    const id = req.params.id;
    const hash = req.params.hash;

    // fazer algo com "id" e "hash" e depois o redirect:
    res.redirect('/forgetPassword');
});
  • 1

    Perfect, thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.