Express.JS:我怎样才能通过名字而不是数字设置回复状态?

好的,大家都知道200是没有问题的,404没有find。 但是,对于永久性与临时性redirect,付款需求或其他更具异国情调的HTTP错误代码,我可能会更好地执行如下操作:

response.status('REQUEST_ENTITY_TOO_LARGE'); 

而不是仅仅使用一个被认为是不好的练习的幻数。 当然,我可以在某些对象中使​​用413:'REQUEST_ENTITY_TOO_LARGE',但Express已经有一个状态码 – >名称映射的副本,我宁愿不重复。

如何在Express JS中按名称指定响应状态?

编辑 :谢谢@Akshat指出http.STATUS_CODES。 详细阐述他的答案,既然价值本身是独一无二的,那么可以运行:

  var statusCodeByName = {}; for ( var number in http.STATUS_CODES ) { statusCodeByName[http.STATUS_CODES[number]] = number } 

这允许人们:

  > statusCodeByName['Request Entity Too Large'] '413' 

有一个Node模块就是为了这个目的:http-status-codes。

https://www.npmjs.org/package/http-status-codes

以下是文档所说的内容:

安装

npm install http-status-codes

用法

 var HttpStatus = require('http-status-codes'); response.send(HttpStatus.OK); response.send( HttpStatus.INTERNAL_SERVER_ERROR, { error: HttpStatus.getStatusText(HttpStatus.INTERNAL_SERVER_ERROR) } ); 

HTTP响应代码不是幻数; 他们是规格。 描述性文本只是一个有用的提示,但协议本身依赖于这些状态代码,而核心代码是非常值得学习的。 两个想法。 你当然可以在你的文件的顶部创build一个常量,并执行此操作:

 var REQUEST_ENTITY_TOO_LARGE = 413; response.status(REQUEST_ENTITY_TOO_LARGE); 

但是,大多数REST API仅实现以下响应:

 200 - OK 201 - Created # Response to successful POST or PUT 302 - Found # Temporary redirect such as to /login 303 - See Other # Redirect back to page after successful login 304 - Not Modified 400 - Bad Request 401 - Unauthorized # Not logged in 403 - Forbidden # Accessing another user's resource 404 - Not Found 500 - Internal Server Error 

最后,如果有帮助,我会分享我们的代码来渲染自定义错误页面:

 module.exports = function(app) { app.use(function(req, res) { // curl https://localhost:4000/notfound -vk // curl https://localhost:4000/notfound -vkH "Accept: application/json" res.status(404); if (req.accepts('html')) { res.render('error/404', { title:'404: Page not found', error: '404: Page not found', url: req.url }); return; } if (req.accepts('json')) { res.send({ title: '404: Page not found', error: '404: Page not found', url: req.url }); } }); app.use( function(err, req, res, next) { // curl https://localhost:4000/error/403 -vk // curl https://localhost:4000/error/403 -vkH "Accept: application/json" var statusCode = err.status || 500; var statusText = ''; var errorDetail = (process.env.NODE_ENV === 'production') ? 'Sorry about this error' : err.stack; switch (statusCode) { case 400: statusText = 'Bad Request'; break; case 401: statusText = 'Unauthorized'; break; case 403: statusText = 'Forbidden'; break; case 500: statusText = 'Internal Server Error'; break; } res.status(statusCode); if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test') { console.log(errorDetail); } if (req.accepts('html')) { res.render('error/500', { title: statusCode + ': ' + statusText, error: errorDetail, url: req.url }); return; } if (req.accepts('json')) { res.send({ title: statusCode + ': ' + statusText, error: errorDetail, url: req.url }); } }); }; 

除非您愿意自己更改源代码,否则这是不可能的。 看看res.send的实现

如果你提供一个string作为参数,它只是将其解释为html,并将响应发送为200。

我认为原因expression式使用HTTP状态码的数字是因为节点本身使用数字作为http.STATUS_CODES的对象键