express req object

来源:互联网 发布:阿里云 404 编辑:程序博客网 时间:2024/04/28 21:36

req.params

// get /user/kyfxbl/29app.get("/user/:id1/:id2", function (req, res) {    console.log(req.params);// [ id1: 'kyfxbl', id2: '29' ]    res.end("hello world");});

req.query

// get /user/kyfxbl/abc?age=29app.get("/user/:id1/:id2", function (req, res) {    console.log(req.query);// { age: '29' }    res.end("the id is: " + req.params.id1);});

req.body

这个是解析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);});

GET /user/kyfxbl/29,将会输出:

{ 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' ] }

req.cookies

cookie信息


req.get()

req.get('User-Agent');// 获取http request header中的信息

req.accepts()

Check if the given types are acceptable, returning the best match when true, otherwise undefined - in which case you should respond with 406 "Not Acceptable".

这个方法比较少用到


req.path

console.log(req.path);

GET /user/kyfxbl/29,返回/user/kyfxbl/29


还有一些特别一目了然的,一起贴了:

req.ip => client(browser) ip

req.host => localhost

req.protocol => http


另外还有一些都是跟http request header相关的查询方法,详见官方reference: express reference

原创粉丝点击