Node.js' Express 中间件在 Nodester 上不返回错误

发布于 2024-11-04 05:08:22 字数 2156 浏览 0 评论 0原文

我的节点应用程序使用express框架。我开发了一个中间件来检查参数,但是当中间件发送错误时,它不会在响应中发回。 我只在节点上遇到这个问题,在本地主机上它按预期工作。

以下是控制器操作的示例:

/*
 * Create a new user
 *
 */
app.post('/user/create', md.checkParams(["nickname", "email", "password"]), function(req, res){
  var email    = req.query.email;
  var nickname = req.query.nickname;
  var password = req.query.password;
  jsonObj = { "nickname" : nickname, "email" : email, "password" : password };

  // Save user in DB
  db.hmset("user:" +nickname, jsonObj, function(err1){
    if(!err1) {
      db.sadd("users","user:" + nickname, function(err2){
        if(err2){
          jsonObj = {"error" : "database error", "message" : "error adding object to set" };
        }
        h.respond(res, jsonObj);
      });
    } else {
      jsonObj = {"error" : "database error", "message" : "error creating hash for object" };
      h.respond(res, jsonObj);
    }
  });
});

checkParams 中间件是:

function checkParams(arr){
  return function(req, res, next) {
    // Make sure each param listed in arr is present in req.query
    var missing_params = [];
    for(var i=0;i<arr.length;i++){
      if(! eval("req.query." + arr[i])){
        missing_params.push(arr[i]);
      }
    }
    if(missing_params.length == 0){
      next();
    } else {
      next(JSON.stringify({ "error" : "query error", "message" : "Parameter(s) missing: " + missing_params.join(",") }));
    } 
  }
}

例如,如果我

curl -XPOST 'http://HOST/users/create'

在本地主机上使用 a,我会得到:

{"error":"query error","message":"Parameter(s) Missing:nickname,email ,password"}

在 NODESTER 上我只得到:

Internal Server Error 

({"error":"query error","message":"Parameter(s) Missing:nickname,email,password"} 出现在应用程序日志中,但不在响应中)

知道可能出了什么问题吗?

更新

我找到了一种解决方法,当应该抛出错误时,在中间件中发出以下命令:

res.writeHead(200, {'content-type': 'application/json'});
res.write("my error message");
res.end();

而不是

next("my error message);

这样工作,即使我不能 100% 确定这是最佳解决方案。

My node app uses the express framework. I developed a middleware to check parameters but when an error is sent by the middleware it is not sent back in the response.
I only have this problem on nodester, on localhost it works as expected.

Here is an example of the controller action:

/*
 * Create a new user
 *
 */
app.post('/user/create', md.checkParams(["nickname", "email", "password"]), function(req, res){
  var email    = req.query.email;
  var nickname = req.query.nickname;
  var password = req.query.password;
  jsonObj = { "nickname" : nickname, "email" : email, "password" : password };

  // Save user in DB
  db.hmset("user:" +nickname, jsonObj, function(err1){
    if(!err1) {
      db.sadd("users","user:" + nickname, function(err2){
        if(err2){
          jsonObj = {"error" : "database error", "message" : "error adding object to set" };
        }
        h.respond(res, jsonObj);
      });
    } else {
      jsonObj = {"error" : "database error", "message" : "error creating hash for object" };
      h.respond(res, jsonObj);
    }
  });
});

The checkParams middleware is:

function checkParams(arr){
  return function(req, res, next) {
    // Make sure each param listed in arr is present in req.query
    var missing_params = [];
    for(var i=0;i<arr.length;i++){
      if(! eval("req.query." + arr[i])){
        missing_params.push(arr[i]);
      }
    }
    if(missing_params.length == 0){
      next();
    } else {
      next(JSON.stringify({ "error" : "query error", "message" : "Parameter(s) missing: " + missing_params.join(",") }));
    } 
  }
}

For instance, if I use a

curl -XPOST 'http://HOST/users/create'

on localhost I get:

{"error":"query error","message":"Parameter(s) missing: nickname,email,password"}

on NODESTER I only got:

Internal Server Error 

({"error":"query error","message":"Parameter(s) missing: nickname,email,password"} appears in the app log but not in the response)

Any idea what could be wrong ?

UPDATE

I found a workaround by issuing the following in the middleware when an error is supposed to be thrown:

res.writeHead(200, {'content-type': 'application/json'});
res.write("my error message");
res.end();

instead of

next("my error message);

it's working this way even if I'm not 100% sure this is best solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多像笑话 2024-11-11 05:08:22

查看您的 Express 配置设置

默认情况下,在“开发”下,服务器返回错误详细信息,而在“部署”下,服务器会隐藏它们。

更新

关于您提出的解决方案。我建议您使用多个非标准参数调用 next,即

next("error", data)

然后在 connect 中使用一个全局中间件,它充当全局错误处理机制,您可以在其中将错误数据写入以您个人的方式回应。

您可能会遇到一些中间件表现奇怪的边缘情况。您必须详细了解 next 的工作原理。

Look at your express configuration settings.

by default under "development" the server returns error details whereas under "deployment" it hides them.

Update

With regards to your proposed solution. I would recommend you call next with multiple non standard parameters i.e.

next("error", data)

And then have a global middleware in connect that acts as a global error handling mechanism where you write error data to the response in your own personal manner.

You may have some edge cases where the middlewares behave strangely. You'll have to look into how next works in detail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文