json-response
API-ish JSON response for express using basic fields (status, data, message). DRY.
The response HTTP status code will ALWAYS be 200; The status on the JSON object will be the corresponding status code.
The JSON response looks like this:
Installation
npm install --save json-response
Usage
This module should only be required once, as soon as possible to the start of your application. It will extend Node's http.ServerResponse.prototype
with HTTP status functions by code and name (see below).
var express = ;var app = ; ; app; app;
If you prefer to send your own responses (most likely using res.send
or res.json
) you can always use res.response
to create a consistent response object.
var express = ;var app = ; ; app; app;
Available Methods
res.response(status, data, message);
Returns an object to be sent later to the client using res.send or similar.
res;
Note: All the bellow methods can be used by either name or code; all of them call automatically res.send
. If you need to add custom headers or any other response you can do so as usual before calling them.
res.ok(data, message)
res; // or res200data message
res.created(data, message)
res; // or res201data message
res.accepted(data, message)
res; // or res202data message
res.nonAuthoritativeInformation(data, message)
res; // or res203data message
res.noContent(data, message)
res; // or res204data message
res.resetContent(data, message)
res; // or res205data message
res.partialContent(data, message)
res; // or res206data message
res.badRequest(data, message)
res; // or res400data message
res.unauthorized(data, message)
res; // or res401data message
res.paymentRequired(data, message)
res; // or res402data message
res.forbidden(data, message)
res; // or res403data message
res.notFound(data, message)
res; // or res404data message
res.methodNotAllowed(data, message)
res; // or res405data message
res.notAcceptable(data, message)
res; // or res406data message
res.proxyAuthenticationRequired(data, message)
res; // or res407data message
res.requestTimeout(data, message)
res; // or res408data message
res.conflict(data, message)
res; // or res409data message
res.gone(data, message)
res; // or res410data message
res.lengthRequired(data, message)
res; // or res411data message
res.preconditionFailed(data, message)
res; // or res412data message
res.requestEntityTooLarge(data, message)
res; // or res413data message
res.requestURITooLong(data, message)
res; // or res414data message
res.unsupportedMediaType(data, message)
res; // or res415data message
res.requestedRangeNotSatisfiable(data, message)
res; // or res416data message
res.expectationFailed(data, message)
res; // or res417data message
res.internalServerError(data, message)
res; // or res500data message
res.notImplemented(data, message)
res; // or res501data message
res.badGateway(data, message)
res; // or res502data message
res.serviceUnavailable(data, message)
res; // or res503data message
res.gatewayTimeout(data, message)
res; // or res504data message
res.httpVersionNotSupported(data, message)
res; // or res505foo:'bar' 'message';
To Do
- Add test cases
License
MIT