1.1.1 • Published 5 years ago

node-response v1.1.1

Weekly downloads
1
License
LGPL-3.0-only
Repository
github
Last release
5 years ago

Node Response

A simple API response handler for Node/Express.

{
	"status": 200,
	"success": true,
	"error": null,
	"data": {
		"hello": ["world"]
	},
	"meta": null
}

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 instance
const express = require('express');
const router = express.Router();

// Add node response middleware
router.use(require('node-response').middleware);

// Set other routes
router.use(require('./routes/auth'));

// Export to be used in application
module.exports = 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

function authenticate(_, res) {
    res.locals.handler.json();
}

module.exports = {
    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 = require('node-response');

// or
const response = require('node-response').response;

(new response).json();

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/auth
const response = require('node-response');

function authenticate(_, res) {
    res.locals.handler.json();
    
    // or
    (new response({ res }).json();
}

module.exports = {
    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

function authenticate(_, res) {
    const object = res.locals.handler.object();
}

module.exports = {
    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:

{
	"status": 200,
	"success": true,
	"error": null,
	"data": null,
	"meta": null
}

Or you can pass in an object (each property is optional):

const success = {
	data, meta, status
};

const object = (new response).success(success).json();

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:

{
	"status": 400,
	"success": false,
	"error": null,
	"data": null,
	"meta": null
}

Or you can pass in an object (each property is optional):

const error = {
	error, status
};

const object = (new response).error(error).json();

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 = (new response).success();

// Overwriting the default status
r.status(201);

// Overwriting the default data
r.data('abc');

// Overwriting the default meta
r.meta({})

// JSON object with new data
const object = r.json();

Overwriting default success object then returning it.