是否可以找回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
object.
res.end(JSON.stringify(req.query, null, 2));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)