(六)、nodejs中的express框架获取http参数

时间:2021-12-10 14:45:10

express获取参数方法:

一、通过req.params

app.get('/user/:id', function(req, res){
res.send('user ' + req.params.id);
});

也可以:

app.get('/user/:id', function(request, response){
response.send('user ' + request.params.id);
});

二、通过req.body

var app = require('express')();
var bodyParser = require('body-parser');
var multer = require('multer'); app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(multer()); // for parsing multipart/form-data app.post('/', function (req, res) {
console.log(req.body);
res.json(req.body);
})

三、通过req.query

// GET /search?q=tobi+ferret
req.query.q
// => "tobi ferret" // GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse
req.query.order
// => "desc" req.query.shoe.color
// => "blue" req.query.shoe.type
// => "converse"