Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JSON.Parse解析请求_Json_Node.js_Express - Fatal编程技术网

使用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提供了一种直接获取数据的方法,那么这应该是首选的方法