在 Node.js POST body / json 上 RESTify
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22208975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RESTify on Node.js POST body / json
提问by Danny BoyWonder
I am in need of help. I am POSTing json data to my node server. The node server is using RESTify for its API. I am having trouble getting req.body.namefrom the body of the posted data.
我需要帮助。我正在将 json 数据发布到我的节点服务器。节点服务器为其 API 使用 RESTify。我无法req.body.name从已发布数据的正文中获取。
The posted data contains a json body. In it i have keys such as name, date, address, email, etc.
发布的数据包含一个 json 正文。其中我有姓名、日期、地址、电子邮件等键。
I want to get the name out of the json body. I am trying to do req.body.namebut it is not working.
我想从 json 正文中获取名称。我正在尝试这样做,req.body.name但它不起作用。
I have also included server.use(restify.bodyParser());and it is not working.
我也包括在内server.use(restify.bodyParser());,但它不起作用。
I am able to req.params.nameand assign a value. But if I POST json data like: {'food': 'ice cream', 'drink' : 'coke'}, I am getting undefined. However, If I do req.body, I get the full json body posted. I want to be able to specifically get an item like 'drink' and have that show on console.log.
我能够req.params.name分配一个值。但是,如果我 POST json 数据如下:{'food': 'ice cream', 'drink' : 'coke'},我会变得未定义。但是,如果我这样做req.body,我会发布完整的 json 正文。我希望能够专门获得像“饮料”这样的项目,并在 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);
});
回答by soundly_typed
If you want to use req.params, you should change:
如果你想使用req.params,你应该改变:
server.use(restify.plugins.bodyParser({ mapParams: false }));
to use true:
使用真:
server.use(restify.plugins.bodyParser({ mapParams: true }));
回答by MForMarlon
Have you tried using the standard JSON library to parse the body as a json object? Then, you should be able to grab whatever property you need.
您是否尝试过使用标准 JSON 库将正文解析为 json 对象?然后,您应该能够获取所需的任何财产。
var jsonBody = JSON.parse(req.body);
console.log(jsonBody.name);
回答by Himanshu sharma
In addition to below answer . The latest syntax in restify 5.0 has been change .
除了下面的答案。restify 5.0 中的最新语法已更改。
All the parser that you are looking for is inside restify.pluginsinstead of restifyuse restify.plugins.bodyParser
您正在寻找的所有解析器都在里面restify.plugins而不是restify使用restify.plugins.bodyParser
The method to use it is this.
使用方法是这样的。
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));
回答by James Shapiro
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);
});
... Is what worked for me with restify version 8.3.2
... 是什么对我有用的 restify 版本 8.3.2
回答by Lucas Burg
you must use req.params with bodyParser active.
您必须在 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);
});

