open source guy
open source guy

Reputation: 2787

How can I pass URL variable to express.js as in php?

Now am converting my php website to express js. There are lot pf script in my front end. All these script generate link like this page.php?id=10&something=anything. In express js, I can catch if the url like this page.php?/10/anything**. Is there any method to catch variable from url like page.php?id=10&something=anything in express js?

Upvotes: 2

Views: 859

Answers (1)

Ito
Ito

Reputation: 2167

You can get using the traditional Express way like "/myroute/id/:number" and also like this "myroute?id=:number"

First: change the route code if your route seems like this:

app.get('/user/:id', user.list);

to

app.get('/user/', user.list);

Inside your users.js change the behavior to get the params...

exports.list = function(req, res){
    var id = req.query.id;
    console.log("ID: " + id);
};

The secret to get the params as you want is "query" in req.query.id.

Upvotes: 1

Related Questions