Code Monkey home page Code Monkey logo

express-last-middleware's Introduction

express-last-middleware

Build Status Coverage Status

The last middleware of express to handle the final response.

Installation

$ npm install express-last-middleware

Features

  • Uniformly use the next() function in express to handle the response
  • Setting the HTTP status code and response body at the same time
  • Custom internal error response handler

Usage

First, use express-last-middleware as the last middleware for express.

const express = require('express');
const lastMiddleware = require('express-last-middleware');

const app = express();

// some routes ...

app.use(lastMiddleware());

app.listen(3000);

Then, when you need to send a response, just pass a value to the next() function. This passed value can be an object or a basic data type value.

app.get('/', (req, res, next) => {
  next({ response: { data: 'OK' } });
});

If you pass a non-error object to the next() function, this object allows three optional attributes.

app.get('/', (req, res, next) => next({
  statusCode: 200,            // set the HTTP status code, default 200
  response: { data: 'OK' },   // set the response body
  format: 'json'              // set the format of the response body, 'json' or 'string', default 'json'
}));

If you pass an error object to the next() function, the default HTTP status code for this response is 500 and the response body is an object containing the error message. You can also customize the function to handle the error object.

app.get('/', (req, res, next) => {
  next(new Error('This is an error'));
});

If you pass a basic data type value to the next() function, the default HTTP status code for this response is 200 and the response body is the value of this basic data type value.

app.get('/', (req, res, next) => {
  next('OK');
});

API

lastMiddleware(options)

This options has two optional attributes, format and handleError.

options.format

The format of the response body, json or string, default json.

options.handleError

The function to handle the error object. If this function is not set, a 500 HTTP status code and error message response body will be returned when the next() function passes in an error object or an internal error occurs.

This function must receive three parameters, error, req and res.

options example

app.use(lastMiddleware({
  format: 'json',
  handleError: (error, req, res) => {
    res.status(500);
    res.json({ message: error.message });
  }
}));

Tests

$ npm test

LICENSE

MIT

express-last-middleware's People

Contributors

royeo avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.