我想在我的Express/Node服务器上模拟404错误.我怎样才能做到这一点?
我的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状态消息?