req.params
// get /user/kyfxbl/29 app.get("/user/:id1/:id2", function (req, res) { console.log(req.params);// [ id1: 'kyfxbl', id2: '29' ] res.end("hello world"); });
// get /user/kyfxbl/abc?age=29 app.get("/user/:id1/:id2", function (req, res) { console.log(req.query);// { age: '29' } res.end("the id is: " + req.params.id1); });
这个是解析http请求体(post)的结果,需要use middleware(express.bodyParser)
req.param(name)
是一种简写,查找顺序依次是params, body, query
req.route
路由的信息:
app.get("/user/:id1/:id2", function (req, res) { console.log(req.route); res.end("the id is: " + req.params.id1); });
{ path: '/user/:id1/:id2', method: 'get', callbacks: [ [Function] ], keys: [ { name: 'id1', optional: false }, { name: 'id2', optional: false } ], regexp: /^\/user\/(?:([^\/]+?))\/(?:([^\/]+?))\/?$/i, params: [ id1: 'kyfxbl', id2: '29' ] }
cookie信息
req.get()
req.get('User-Agent');// 获取http request header中的信息
Check if the giventypes
are acceptable, returning the best match when true, otherwiseundefined
- in which case you should respond with 406 "Not Acceptable".
这个方法比较少用到
req.path
console.log(req.path);
还有一些特别一目了然的,一起贴了:
req.ip => client(browser) ip
req.host => localhost
req.protocol => http
另外还有一些都是跟http request header相关的查询方法,详见官方reference:express reference