Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.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
Node.js Curling Express服务器因多个参数而失败_Node.js_Curl_Express - Fatal编程技术网

Node.js Curling Express服务器因多个参数而失败

Node.js Curling Express服务器因多个参数而失败,node.js,curl,express,Node.js,Curl,Express,我试图卷曲我的本地服务器来创建一条消息,但遇到了奇怪的错误。我可以使用curl创建一个用户: $curl --data userToken='awdawd' localhost:3000/usertoken >>>created 但是,当我尝试将邮件发送到/路由时,我得到以下信息: $curl -data x="10"&y="10"&z="10"&message="hello"&userToken="mrTony" http://localho

我试图卷曲我的本地服务器来创建一条消息,但遇到了奇怪的错误。我可以使用curl创建一个用户:

$curl --data userToken='awdawd' localhost:3000/usertoken
>>>created
但是,当我尝试将邮件发送到/路由时,我得到以下信息:

$curl -data x="10"&y="10"&z="10"&message="hello"&userToken="mrTony" http://localhost:3000/messages

>>>[1] 26248
>>>[2] 26249
>>>[3] 26250
>>>[4] 26251
>>>-bash: http://localhost:3000/messages: No such file or directory
>>>[2]   Done                    y="10"
>>>[3]-  Done                    z="10"
>>>[4]+  Done                    message="hello"
>>>curl: (6) Could not resolve host: x=10

>>> [1]+  Exit 6                  curl -data x="10"
我认为可能存在内容类型问题,所以我尝试将内容类型包括为application/json,但仍然会出现相同的错误

以下是我的express服务器路由:

  //input: {userToken: string}
  router.post('/usertoken', function (req, res) {
    var token = req.body.userToken;
    models.createUser(token).then(
      util.resolvePOST.bind(this, req, res),
      util.rejectPOST.bind(this, req, res)
      );
  });

  //input: {x: float, y: float, z: float, message: string, userToken: string}
  router.post('/messages', function(req, res) {
    console.log('does not work', req.body);
    models.createMessage(req.body).then(
      util.resolvePOST.bind(this, req, res),
      util.rejectPOST.bind(this, req, res)
      );
  });

问题不在于你的express应用程序,而在于你调用curl的方式。您的shell正在查看
&
,并将内容放在后台。因此,您需要确保它们都包含在引号中,以便shell不会处理它们

请尝试以下方法:

curl "http://localhost:3000/messages?x=10&y=10&z=10&message=hello&userToken=mrTony"
您行中的“&”使linux感到困惑。基本上,当您在命令行中使用它时,它会告诉它将该命令作为后台进程运行

你需要把所有的东西都用引号括起来,你试过了吗

$curl -data 'x="10"&y="10"&z="10"&message="hello"&userToken="mrTony"' http://localhost:3000/messages

为了进一步了解这里的两个正确答案,您应该能够将反斜杠转义为“与”

$curl -data x="10"\&y="10"\&z="10"\&message="hello"\&userToken="mrTony" http://localhost:3000/messages