相关疑难解决方法(0)

如何以编程方式使用Express/Node发送404响应?

我想在我的Express/Node服务器上模拟404错误.我怎样才能做到这一点?

javascript node.js express

168
推荐指数
5
解决办法
13万
查看次数

如何在node/express中发送自定义http状态消息?

我的node.js应用程序的建模类似于express/examples/mvc应用程序.

在控制器操作中,我想用自定义http消息吐出HTTP 400状态.默认情况下,http状态消息为"错误请求":

HTTP/1.1 400 Bad Request
Run Code Online (Sandbox Code Playgroud)

但我想发送

HTTP/1.1 400 Current password does not match
Run Code Online (Sandbox Code Playgroud)

我尝试了各种方法,但没有一个将http状态消息设置为我的自定义消息.

我当前的解决方案控制器功能如下所示:

exports.check = function( req, res) {
  if( req.param( 'val')!=='testme') {
    res.writeHead( 400, 'Current password does not match', {'content-type' : 'text/plain'});
    res.end( 'Current value does not match');

    return;
  } 
  // ...
}
Run Code Online (Sandbox Code Playgroud)

一切正常,但......似乎不是正确的方法.

有没有更好的方法来使用快递设置http状态消息?

node.js express

74
推荐指数
6
解决办法
8万
查看次数

标签 统计

express ×2

node.js ×2

javascript ×1