在Node.js POST body/json上恢复
我需要帮助。我正在将json数据发布到我的节点服务器。节点服务器正在使用RESTify作为其API。我无法从发布数据的主体中获取在Node.js POST body/json上恢复,json,node.js,restify,Json,Node.js,Restify,我需要帮助。我正在将json数据发布到我的节点服务器。节点服务器正在使用RESTify作为其API。我无法从发布数据的主体中获取req.body.name 发布的数据包含一个json主体。在里面我有诸如姓名、日期、地址、电子邮件等钥匙 我想从json主体中获取名称。我正在尝试执行req.body.name,但它不起作用 我还包括了server.use(restify.bodyParser())并且它不工作 我能够req.params.name并赋值。但是如果我发布json数据,比如:{'food
req.body.name
发布的数据包含一个json主体。在里面我有诸如姓名、日期、地址、电子邮件等钥匙
我想从json主体中获取名称。我正在尝试执行req.body.name
,但它不起作用
我还包括了server.use(restify.bodyParser())代码>并且它不工作
我能够req.params.name
并赋值。但是如果我发布json数据,比如:{'food':'ice cream','drink':'coke'}
,我就没有定义。但是,如果我做了req.body
,我会发布完整的json body。我希望能够特别获得像“饮料”这样的物品,并将其显示在console.log上
var restify = require('restify');
var server = restify.createServer({
name: 'Hello World!',
version: '1.0.0'
});
server.use(restify.acceptParser(server.acceptable));
server.use(restify.jsonp());
server.use(restify.bodyParser({ mapParams: false }));
server.post('/locations/:name', function(req, res, next){
var name_value = req.params.name;
res.contentType = 'json';
console.log(req.params.name_value);
console.log(req.body.test);
});
server.listen(8080, function () {
console.log('%s listening at %s', server.name, server.url);
});
您是否尝试过使用标准JSON库将主体解析为JSON对象?然后,你应该能够抓住任何你需要的财产
var jsonBody = JSON.parse(req.body);
console.log(jsonBody.name);
如果要使用req.params
,应更改:
server.use(restify.plugins.bodyParser({ mapParams: false }));
要使用true,请执行以下操作:
server.use(restify.plugins.bodyParser({ mapParams: true }));
必须在bodyParser处于活动状态时使用req.params
var restify = require('restify');
var server = restify.createServer({
name: 'helloworld'
});
server.use(restify.bodyParser());
server.post({path: '/hello/:name'}, function(req, res, next) {
console.log(req.params);
res.send('<p>Olá</p>');
});
server.get({path: '/hello/:name', name: 'GetFoo'}, function respond(req, res, next) {
res.send({
hello: req.params.name
});
return next();
});
server.listen(8080, function() {
console.log('listening: %s', server.url);
});
var restify=require('restify');
var server=restify.createServer({
名称:“helloworld”
});
use(restify.bodyParser());
post({path:'/hello/:name'},函数(req,res,next){
控制台日志(请求参数);
res.send('p>Olá');
});
get({path:'/hello/:name',name:'GetFoo'},函数respond(req,res,next){
res.send({
您好:req.params.name
});
返回next();
});
listen(8080,函数(){
console.log('侦听:%s',server.url);
});
除了下面的答案。restify 5.0中的最新语法已经更改
您要查找的所有解析器都在restify.plugins
中,而不是restify
使用restify.plugins.bodyParser
使用它的方法是这样的
const restify = require("restify");
global.server = restify.createServer();
server.use(restify.plugins.queryParser({
mapParams: true
}));
server.use(restify.plugins.bodyParser({
mapParams: true
}));
server.use(restify.plugins.acceptParser(server.acceptable));
。。。restify 8.3.2版对我有效吗?req.body.test
的值是多少?如果是这样,你应该应用内容类型:application/json
来请求标题,这样restify就可以自动完成。@Phoenix你应该添加它作为答案,这样我就可以投票了。就像一个符咒。不需要“restify插件”,它们都已经在restify.plugins
中了。是的,现在又在restify中了。我会更新我的答案。
var restify = require('restify')
const restifyBodyParser = require('restify-plugins').bodyParser;
function respond(req, res, next) {
console.log(req.body)
const randomParam = req.body.randomParam
res.send(randomParam);
next();
}
var server = restify.createServer();
server.use(restifyBodyParser());
server.post('/hello/:name', respond);
server.head('/hello/:name', respond);
server.listen(8080, function() {
console.log('%s listening at %s', server.name, server.url);
});