Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express.js: how to make app.get('/[:userId]i'..) in req.param?

I m using nodejs 0.8.21 and express 3.1.0

I need to read userId from url like http://mysite.com/39i. It means userId=39. How to do? Thank you. Sample:

app.get('/:userId_i', function(req, res) { });
app.param('userId', function(req, res, next, id){ });
like image 948
sirjay Avatar asked Mar 18 '13 17:03

sirjay


People also ask

What is req params ID in Express?

The req. params property is an object containing properties mapped to the named route “parameters”. For example, if you have the route /student/:id, then the “id” property is available as req.params.id. This object defaults to {}. Syntax: req.params.

How do you access GET parameters after Express?

Your query parameters can be retrieved from the query object on the request object sent to your route. It is in the form of an object in which you can directly access the query parameters you care about. In this case Express handles all of the URL parsing for you and exposes the retrieved parameters as this object.

How do you use handle GET request in Express JS?

var express = require('express'); var app = express(); app. get("/page/:id",function(request, response){ var id = request.params.id; // do something with id // send a response to user based on id var obj = { id : id, Content : "content " +id }; response. writeHead(200, {"Content-Type": "application/json"}); response.

What is var Express require (' Express ')?

var express = require('express'); => Requires the Express module just as you require other modules and and puts it in a variable. var app = express(); => Calls the express function "express()" and puts new Express application inside the app variable (to start a new Express application).


1 Answers

Assuming you have a numerical id followed by an i and you want the id coming back as just the numerical.

app.get("/:id([0-9]+)i", function (req, res) {...});

This will match a numeric followed by an i and will give you just the numeric in req.params.id.

Example:

curl -XGET localhost:8080/124i

Gives me the following in req.params

[ 'id': 124 ]
like image 91
travis Avatar answered Sep 25 '22 06:09

travis