是否可以使用检索body
内容express
?
我从尝试开始,body-parser
但是似乎不起作用GET
。是否有任何可行的模块?
var express = require('express'),
bodyParser = require('body-parser'),
PORT = process.env.PORT || 4101,
app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.route('/')
.get(function(req, res) {
respond(req, res, 'GET body contents:\n');
})
.post(function(req, res) {
respond(req, res, 'POST body contents:\n');
});
app.listen(PORT, function(err) {
if (err) {
console.log('err on startup ' + err);
return;
}
console.log('Server listening on port ' + PORT);
});
/*
* Send a response back to client
*/
function respond(req, res, msg){
res.setHeader('Content-Type', 'text/plain');
res.write(msg);
res.end(JSON.stringify(req.body, null, 2));
}
这是来自的回复GET
:
GET body contents:
{}
来自POST
:
POST body contents:
{
"gggg": ""
}
GET请求没有正文,它们具有查询字符串。为了访问expressJS中的查询字符串,您应该使用req.query
对象。
res.end(JSON.stringify(req.query, null, 2));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句