
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
http-responses
Advanced tools
Middleware for standardizing the way you send HTTP response statuses.
Prevents inconsistencies in your code by standardizing the way you handle sending HTTP response statuses and bodies.
res.status(401)
res.send(401, body)
res.json(401, body)
next({ status: 401, message: body })
next({ code: 401, message: body })
next(401)
// WWW Response
return next(new res.Unauthorized(body))
// API response
return next(new res.Unauthorized({
body: body
});
$ npm install http-responses
Express.js, before routing
app.use(require('http-responses'))
Semi-real world example, fetching specific user w/ identifier key for an API.
app.use('/:id', function (req, res, next) {
if (!req.param('id', false)) {
return next(new res.Conflict({
body: 'Id is required.'
}));
}
User.find({ _id: req.param('id') }).then(function (user) {
if (user) {
return res.ok('User', user.toJSON(), true);
}
return next(new res.NotFound({
body: 'User does not exist'
}));
}).then(function (err) {
if (err) {
next(new res.InternalServerError({
body: err.message
}));
}
});
});
Handling errors in express should be done the correct way by defining a middleware that contains an arity of four by including the error argument after your routing has been done. By doing this, anything passed through the next argument will be sent here and we can determine what to do from there, here is a generic example of supporting both API / WWW errors.
var xml = require('js2xmlparser');
app.use(function (err, req, res, next) {
res.status(err.status);
if (typeof err.message === 'object') {
return res.format({
json: function () {
res.json({
code: err.code,
message: err.message.body
})
},
html: function () {
res.set('Content-Type', 'application/xml').send(
xml(err.message.type || 'ApiError', {
code: err.code,
message: err.message.text
})
);
}
});
}
res.format({
json: function () {
res.json({
code: err.code,
message: err.message
});
},
html: function () {
res.render('error/index', {
status: err.status,
code: err.code,
message: err.message
});
}
});
});
res.Continue()res.SwitchingProtocols(String protocols)res.Processing()200: res.ok(String view, Mixed body, Boolean api)
view (WWW: partials/user/profile / API: "User")
WWW view path / API XML object type.
Can be omitted when
apiisfalse, HTML requests will returntext/plainbody as no view is declared.
body (user.toJSON())
Response body, here we retrieve the user json output
api (true)
Determines whether view is an HTML template path or an XML object property
<User></User>
You can also pass a method that will be invoked if you want to resolve formats yourself:
res.ok(function () {
res.json(user.toJSON());
})
204: res.NoContent()
res.redirect - native express method.res.MovedPermanently(location)res.Found(location)res.TemporaryRedirect(location)res.PermanentRedirect(location)Properties
code - optional, sub status-code (iis style); defaults to status code.
message - required, mixed type can be string, object, number, date, etc...
Methods
new res.BadRequest([code, ]message)new res.Unauthorized([code, ]message)new res.PaymentRequired([code, ]message)new res.Forbidden([code, ]message)new res.NotFound([code, ]message)new res.MethodNotAllowed([code, ]message)new res.NotAcceptable([code, ]message)new res.ProxyAuthenticationRequired([code, ]message)new res.RequestTimeout([code, ]message)new res.Conflict([code, ]message)new res.LengthRequired([code, ]message)new res.PreconditionFailed([code, ]message)new res.PayloadTooLarge([code, ]message)new res.URITooLong([code, ]message)new res.UnsupportedMediaType([code, ]message)new res.RangeNotSatisfied([code, ]message)new res.ExpectationFailed([code, ]message)new res.ImATeapot([code, ]message)new res.Locked([code, ]message)new res.PreconditionRequired([code, ]message)new res.TooManyRequests([code, ]message)new res.InternalServerError([code, ]message)new res.NotImplemented([code, ]message)new res.BadGateway([code, ]message)new res.ServiceUnavailable([code, ]message)new res.GatewayTimeout([code, ]message)new res.HTTPVersionNotSupported([code, ]message)new res.InsufficientStorage([code, ]message)new res.LoopDetected([code, ]message)new res.NotExtended([code, ]message)new res.NetworkAuthenticationRequired([code, ]message)Special Methods
res.UpgradeRequired(String protocols,[code, ]message)FAQs
Middleware for standardizing the way you send HTTP response statuses.
The npm package http-responses receives a total of 9 weekly downloads. As such, http-responses popularity was classified as not popular.
We found that http-responses demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.