1

我在 Node.js/Express.js 中有以下代码

// Post a comment
app.post('/:id/comment', function(req, res){
    var snipp_id = req.params.id;
    var comment = req.body.comment;
    var line_num = req.body.line;

    models.Snipp.findById(snipp_id, function(err, snipp){
        snipp.comments.push({body: comment,line: line_num});
        res.send('OK');
        snipp.save();
    });
});

当我做我的卷曲时:curl -X POST -H '{"comment":"test", "line":2}' http://localhost:3000/51dd25a56416c53f66000002/comment

我收到此错误:

SyntaxError: Unexpected token t
    at Object.parse (native)
    at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/express/node_modules/connect/lib/middleware/json.js:76:27)
    at IncomingMessage.EventEmitter.emit (events.js:92:17)
    at _stream_readable.js:910:16
    at process._tickCallback (node.js:415:13)

我究竟做错了什么?!:)

谢谢!

4

1 回答 1

1

在 curl 命令中设置--header "Content-Type:application/json"并使用-d标志发送数据

因此:

curl -X POST -H "Content-Type:application/json" -d '{"comment":"test", "line":2}' http://localhost:3000/51dd25a56416c53f66000002/comment
于 2013-07-10T11:54:42.177 回答