快速nodejs中的解析器错误
我使用以下数据在Node上打击Web服务,
我的请求数据是:
{
"first_name":"surinder",,
"last_name":"rawat",
"email":"surinder.rawat@testcompany.com",
"phone":"1234567890",
"password":"surinder",
"user_type":"H",
"device_type":"A"
}
并得到以下错误:
Connect
400 SyntaxError: Unexpected token ,
at Object.parse (native)
at parse (/home/surinder/workspace/HappyHakka/node_modules/body-parser/lib/typs
/json.js:76:17)
at /home/surinder/workspace/HappyHakka/node_modules/body-parser/lib/read.js:98:18
at IncomingMessage.onEnd (/home/surinder/workspace/HappyHakka/node_modules/body-parser
/node_modules/raw-body/index.js:136:7)
at IncomingMessage.g (events.js:180:16)
at IncomingMessage.emit (events.js:92:17)
at _stream_readable.js:943:16
at process._tickCallback (node.js:419:13)
我故意用双逗号来得到这个错误。 我想知道如何处理这个错误,并以正确的格式向用户显示错误
谢谢
从文档 -
您可以在其他app.use()
和路由调用之后最后定义错误处理中间件; 例如:
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(function(err, req, res, next) {
// error handling logic
console.error(err.stack);
res.status(500).send('Something broke!');
});
链接地址: http://www.djcxy.com/p/83005.html