使用JSON.Parse解析请求
我尝试使用使用JSON.Parse解析请求,json,node.js,express,Json,Node.js,Express,我尝试使用node.js解析一个请求,因此我构建了下一个: app.get('/download', function(req, res){ var parseJson = JSON.parse(req); var file = parseJson.fileName; res.download(file); // Set disposition and send it. }); 我用高级Rest客户端发送一个请求: URL:http://local
node.js
解析一个请求,因此我构建了下一个:
app.get('/download', function(req, res){
var parseJson = JSON.parse(req);
var file = parseJson.fileName;
res.download(file); // Set disposition and send it.
});
我用高级Rest客户端发送一个请求:
URL:http://localhost:5000/download
方法:GET
原始参数:fileInfo:“C://1//239698\u n.jpg”
这给了我下一个错误:
SyntaxError: Unexpected token o
at Object.parse (native)
at port (c:\Node\nodejs\express.js:13:24)
at callbacks (c:\Node\nodejs\node_modules\express\lib\router\index.js:164:37)
at param (c:\Node\nodejs\node_modules\express\lib\router\index.js:138:11)
at pass (c:\Node\nodejs\node_modules\express\lib\router\index.js:145:5)
at Router._dispatch (c:\Node\nodejs\node_modules\express\lib\router\index.js:173:5)
at Object.router (c:\Node\nodejs\node_modules\express\lib\router\index.js:33:10)
原因是什么?您没有发送JSON。您正在发送querystring格式的参数。你会想要使用或者,不知羞耻的插件提醒,我的模块是格式不可知的。你试过记录
请求
并确认它是有效的JSON吗?如果你使用的是express,你可以跳过JSON.parse
步骤。我不认为您想要的数据在req
中,而是req.query
这可能是一个比下面我的答案更好的答案,如果Express提供了一种直接获取数据的方法,那么这应该是首选的方法