Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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 Node Express未发送自定义错误消息_Node.js_Express_Post_Module_Request - Fatal编程技术网

Node.js Node Express未发送自定义错误消息

Node.js Node Express未发送自定义错误消息,node.js,express,post,module,request,Node.js,Express,Post,Module,Request,我正在尝试制作一个npm模块,其中用户需要发送他的令牌和id才能连接 这是模块代码: var https = require('https'); var fetch = require('node-fetch') module.exports = class Cloud_client { constructor(client,token){ this.token = token ; this.client = client; if(!token) r

我正在尝试制作一个npm模块,其中用户需要发送他的令牌和id才能连接

这是模块代码:

var https = require('https');
var fetch = require('node-fetch')


module.exports = class Cloud_client {
   constructor(client,token){
      this.token = token ;
      this.client = client;
      if(!token) return console.error(`[cdl.js] No token provided`);
      if(!client)  return console.error(`[cdl.js] No Client Id provided`);
   }
  login() {
     fetch(`https://www.cloudlist.xyz/isvalid/${this.token}`, {
        method: "POST",
        headers: {
        "Content-Type": "application/json"
        },
        body:JSON.stringify({client:this.client}),
          }).then(async response => {
            console.log(response)
    return console.error(`[cdl.js] ${response.statusText}`); // eslint-disable-line no-console

    })
  }
}
服务器端邮件处理程序:

    app.post("/isvalid/:token", limiter ,async (req, res) => { 
  if(!req.body.client) return res.status(204).send({message: 'No id provided'});
  if(!req.params.token ) return res.status(204).send({message: 'No token provided'});
database.ref(`Bots/${req.body.client}`).once("value",function(data) {
  if(data.val() === null) return res.status(204).send({message: 'Invalid user'});
  if(!data.val().apiKey) return res.status(204).send({message: "This Client doesn't have an api key yet"});
  if(data.val().apiKey === req.params.token) {
    res.status(200 ).send({message: `Logged in as ${data.val().bot_name}`});
  }else {
    return res.status(401).send({message:"Incorrect authorization token."});   
  } 
})
})
我真的收到了一条错误消息 但此消息来自代码,在本例中是401,而不是自定义消息“不正确的授权令牌”

当我试图给出response.body.message或response.message时,我没有定义,我无法接收错误代码。我尝试了
返回res.status(401).json({message:“不正确的授权令牌”})

但我再次收到状态错误代码

我收到了相同的问题。如果我发送了
res.status(401).json({message:“不正确的授权令牌”})
我将返回
您没有查看此目录或页面的权限

我的问题是我使用iisnode通过IIS运行nodejs。 默认情况下,IIS将替换您发送的任何错误消息。通过将以下代码添加到
()下的web.config文件中,可以关闭此行为:


重新启动服务器后,我能够取回
{message:“不正确的授权令牌”}

问题是,如果Express中出现错误,您必须使用正确的命名约定

您必须使用
errorMessage
错误响应中不会出现任何内容(尽管如果不使用errorMessage,您可能仍然会在网络响应中发现某些内容,这让我觉得Express已损坏)

res.status(401).send({errorMessage:“这是我的自定义消息”})

res.status(401).send({errorMessage:{message:“这是我的自定义对象”,值:32})

希望这有帮助:p