Express.JS: how to set the response status by name, not by number?

OK, everyone knows that 200 is fine, and 404 is not found. But for things like permanent or temporary redirects or a required payment or other more exotic HTTP error codes, it’s better to do something like:

response.status('REQUEST_ENTITY_TOO_LARGE'); 

Instead of just using a magic number, which is usually considered bad practice. I could, of course, have 413: "REQUEST_ENTITY_TOO_LARGE" in some object, but Express already has a copy of the status code β†’ name matching, and I would not want to duplicate it.

How to indicate the response status by name in Express JS?

Edit : thanks @ For specifying http.STATUS_CODES. Developing your answer, since the values ​​themselves are unique, you can run:

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

This allows:

  > statusCodeByName['Request Entity Too Large'] '413' 
+7
express magic-numbers
source share
3 answers

To do this, use the Node module: http-status-codes.

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

This is what the documentation says:

Installation

npm install http-status-codes

Using

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

HTTP response codes are not magic numbers; this is a specification. The descriptive text is just a useful reminder, but the protocol itself relies on these status codes, and the main ones are very valuable. Two thoughts. You can create a constant at the top of the file and do the following:

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

However, most REST APIs simply implement the following answers:

 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 resource 404 - Not Found 500 - Internal Server Error 

Finally, if it is useful, I will share our code for creating custom error pages:

 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 }); } }); }; 
+3
source share

This is not possible if you do not want to change the source code yourself. Take a look at the res.send implementation

If you provide a string as an argument, it simply interprets it as html and sends the response as 200.

I think the reason express uses numbers for HTTP status codes is because node itself uses numbers as object keys for http.STATUS_CODES

+1
source share

All Articles