Node Response
A simple API response handler for Node/Express.
Example response from API.
Installation
node-response
can be installed using NPM or Yarn.
NPM
npm install node-response
Yarn
yarn add node-response
Usage
Examples will show how to use node-response
with Express, but as long as the HTTP response object contains these methods:
status
json
end
Then you can just plug and play.
Setting up middleware
Express allows middleware to be added on the application level or the router level, this means that any routes that extend from the highest source using the middleware will inherit the response. Read more about using middleware here.
// router // Create router instanceconst express = ;const router = express; // Add node response middlewarerouter; // Set other routesrouter; // Export to be used in applicationmoduleexports = router;
Adding the node-response
middleware on the router level.
Accessing the response
When accessing the response through after setting up the middleware it can be found in the res.locals
object as handler
.
// ./routes/auth { reslocalshandler;} moduleexports = authenticate
Returning JSON from the response instance created by the middleware.
If you need to access the response directly you can do so two different ways:
const response = ; // orconst response = response; ;
Creating new response objects and returning JSON.
Returning a JSON HTTP Response
To return a JSON HTTP Response you must pass the Express Response into the API handler, or if you are using the middleware then this is done for you.
// ./routes/authconst response = ; { reslocalshandler; // or res ;} moduleexports = authenticate
Returning a JSON HTTP Response.
If the API response does not have an Express Response then it will return the JSON rather than outputting it as a HTTP response.
Accessing the JSON object
If you still need access the JSON object, then you can call object
instead.
// ./routes/auth { const object = reslocalshandlerobject;} moduleexports = authenticate
Accessing the JSON object even when a Express Response is set.
Creating successful responses
After creating an instance of Response
you are then able to call success
which as the following default response:
Or you can pass in an object (each property is optional):
const success = data meta status; const object = ;
Returning a successful response.
Creating unsuccessful responses
After creating an instance of Response
you are then able to call error
which as the following default response:
Or you can pass in an object (each property is optional):
const error = error status; const object = ;
Returning a unsuccessful response.
Setting individual properties
If for whatever reason you need to set specific properties (status, data, meta) after initially setting what type of response, then you can do:
const r = ; // Overwriting the default statusrstatus201; // Overwriting the default datardata'abc'; // Overwriting the default metar // JSON object with new dataconst object = r;
Overwriting default success object then returning it.