Node.js Axios请求总是发回200

Node.js Axios请求总是发回200,node.js,reactjs,express,axios,Node.js,Reactjs,Express,Axios,出于某种原因,无论我从express server axios返回什么响应代码,我都会发出一个非常简单的PUT请求(见下文)。在我的express日志中,它显示了正确的响应: PUT /v1/org/password/reset 404 339.841 ms - 71 这是发送响应服务器端的代码行: res.json({ message: 'Could not find user with that token or the token expired.' }).status(404); 我

出于某种原因,无论我从express server axios返回什么响应代码,我都会发出一个非常简单的PUT请求(见下文)。在我的express日志中,它显示了正确的响应:

PUT /v1/org/password/reset 404 339.841 ms - 71
这是发送响应服务器端的代码行:

res.json({ message: 'Could not find user with that token or the token expired.' }).status(404);
我知道这行被触发是因为登录到浏览器控制台的主体显示了相同的消息。见截图

我唯一能想到的是浏览器正在缓存响应

    axios.put(`http://localhost:3000/v1/org/password/reset`, {
     password: "example",
     token: "19u8e8j2039d3d32blahblah"
   })
 .then(function (response) {
   if(response.status == 200){
       console.log("success",response)
     //handle success here
   }
 })
 .catch(function (error) {
   console.log("error", error);
   //handle error here
 });

问题出在后端。尝试以这种方式返回响应:

res.status(404).json({ message: 'Could not find user with that token or the token expired.' });
根据Express 4文件-

中间件功能是按顺序执行的,因此 中间件包含非常重要


因此,当您调用“send”方法时,Express会将http正文内容写入响应,这就是为什么首先需要填充标题。

问题出在后端。尝试以这种方式返回响应:

res.status(404).json({ message: 'Could not find user with that token or the token expired.' });
根据Express 4文件-

中间件功能是按顺序执行的,因此 中间件包含非常重要


因此,当您调用“send”方法时,Express会将http正文内容写到响应中,这就是为什么首先需要填充标题。

尝试这样设置状态

res.status(404).json({ message: 'Could not find user with that token or the token expired.' });

试着先这样设置状态

res.status(404).json({ message: 'Could not find user with that token or the token expired.' });

这就解决了!知道为什么顺序在那里很重要吗?有些函数是可链接的,有些不是。json()实际上触发了响应。status()是可链接的。因此,首先调用.json()不会返回可链接的方法,而是默认为状态代码200。这就解决了这个问题!知道为什么顺序在那里很重要吗?有些函数是可链接的,有些不是。json()实际上触发了响应。status()是可链接的。因此,首先调用.json()不会返回可链接的方法,而是默认为状态代码200。