Node.js express.json中间件未按预期解析请求

aka*_*nom 16 javascript json node.js express

我有一个简单的cURL(我认为这是正确的),它将一个小的JSON对象发布到我的快速服务器:

curl -d "{'test': 'this'}" localhost:3000/rest/user/authenticate
Run Code Online (Sandbox Code Playgroud)

我已明确表示:

// set up body parsing in express  to be able  to get parse JSON posts
server.use(express.json());
server.use(express.urlencoded());
Run Code Online (Sandbox Code Playgroud)

并拥有接受路由的处理程序:

JSON = require('JSON')
module.exports = {
    authenticateUser: function create(req, res){
        var postedObject = req.body
        console.log(postedObject)
        res.send('Handle Post: authenticateUser');
    }
}
Run Code Online (Sandbox Code Playgroud)

处理程序被调用,但它正在意外地记录JSON主体:

{ '{\'test\': \'this\'}': '' }
Run Code Online (Sandbox Code Playgroud)

所以我的整个对象看起来是JSON Name:Value对象的名称一边.无论我发布什么,它似乎都附加了价值方面.除非我这样做:

curl -d "a=a" localhost:3000/rest/user/authenticate
Run Code Online (Sandbox Code Playgroud)

哪些日志:

{'a':'a'}
Run Code Online (Sandbox Code Playgroud)

所以我没有设置正确的标题?配置快递错了吗?我计划挖掘快速代码,但想知道在找到解决方案之前是否有人可能知道.无论哪种方式,在网络上有一个可搜索/索引的答案将是很好的.

更新1

好的,我需要将标头添加到cURL

curl -H "Content-Type: application/json" -d "{'test': 'this'}" localhost:3000/rest/user/authenticate
Run Code Online (Sandbox Code Playgroud)

这给出了错误:

Parsing: {'test': 'this'}
SyntaxError: Unexpected token '
    at Object.parse (native)
    at C:\blah\node_modules\express\node_modules\connect\lib\middleware\json.js:86:19
        at IncomingMessage.onEnd (C:blah\node_modules\express\node_modules\connect\node_modules\raw-body\index.js:109:7)
        at IncomingMessage.g (events.js:180:16)
        at IncomingMessage.EventEmitter.emit (events.js:92:17)
        at _stream_readable.js:920:16
        at process._tickCallback (node.js:415:13)
Run Code Online (Sandbox Code Playgroud)

OR curl -H"Content-Type:application/json"-d"{test:'this'}"localhost:3000/rest/user/authenticate

这给出了错误:

Parsing: {test: 'this'}
SyntaxError: Unexpected token t
    at Object.parse (native)
    at C:\blah\node_modules\express\node_modules\connect\lib\middleware\json.js:86:19
        at IncomingMessage.onEnd (C:blah\node_modules\express\node_modules\connect\node_modules\raw-body\index.js:109:7)
        at IncomingMessage.g (events.js:180:16)
        at IncomingMessage.EventEmitter.emit (events.js:92:17)
        at _stream_readable.js:920:16
        at process._tickCallback (node.js:415:13)
Run Code Online (Sandbox Code Playgroud)

更新2

在connect/lib/middleware/json.js文件中

这条线似乎是导致问题的那条线

req.body = JSON.parse(buf, options.reviver);
Run Code Online (Sandbox Code Playgroud)

更新3

我真的认为这是我的cURL

buf= JSON.stringify({test: 'This'});
console.log(buf)
req.body = JSON.parse(buf, options.reviver);
Run Code Online (Sandbox Code Playgroud)

首先记录{"test":"this"}

然后在我的处理程序中:

----------------------
{ test: 'this' }
----------------------
Run Code Online (Sandbox Code Playgroud)

Ole*_*leg 25

1)JSON中间件仅在请求具有Content-Type: application/json头部时才起作用.
2)有效的JSON应该包含",而不是'.
所以它应该是'{"test": "this"}'代替"{'test': 'this'}"

试试这个命令:

curl -d '{"test": "this"}' -H "Content-Type: application/json" localhost:3000/rest/user/authenticate
Run Code Online (Sandbox Code Playgroud)


aka*_*nom 6

答案有两个部分

  1. 添加内容标题
  2. 在Windows上经历了正确传递json的痛苦,我的Windows机器上的cURL不处理单引号和双引号的交换,因此正确的cURL是

    curl -H"Content-Type:application/json"-d"{"""test""":"""this"""}"localhost:3000/rest/user/authenticate

是的,在数据​​参数内部,您需要使用三个双引号将单个双引号发送到服务器.

接受zub的回答,因为他是正确的.我正在尝试一堆东西绕过这里的窗户.