Skip to content

algo7/express-suite

Repository files navigation

express-suite

A collection of Middlewares for Express

Maintainability Codacy Badge npm version

:) More middlewares will be available as the time progresses.

Installation

$ npm i express-suite 

routeCheck

This middleware will handle all requests to non-registered routes for you.

In the config option, you can specify a route to handle all requests to non-existing routes. For example, a custom page-not-found notice.

If no config option is passed, the middleware will send a 404 to those requests.

//Dependencies const express = require('express'); //Require the routeCheck Middleware const { routeCheck } = require('express-suite'); //Initialize the App const app = express(); //Load Routes const example = require('./routes/example'); //Use Routes app.use('/', example); //The routeCheck middleware config option //The redirectPath refers to the custom route you want to redirect the requests to const options = { redirectPath: '/PnF' }; //It's important to use the routeCheck middleware after all routes are loaded app.use(routeCheck(app, options)); //Start the server app.listen(5003);

emptyInputCheck

This middleware checks for all empty inputs to all routes (unless used at a router level) by checking the body of both POST and GET requests.

Note: 0 does count as a valid input.

Default Error Messages:

  1. Requests with an empty body will be met with a 400 and the message:
{ msg: 'The request body is empty!'; }
  1. Request with empty fields in the body will be met with a 400 and the message:
{ msg: 'The request body is empty!', field:'key of the missing field' }
//supressFieldKey=true { msg: 'The request body is empty!', }

You can specify the middleware to skip the check for GET requests in the config option.

Custom error message for each scenario above and whether to supress the filed key can be specified like the example below:

//Dependencies const express = require('express'); const bodyParser = require('body-parser'); //Require the routeCheck Middleware const { emptyInputCheck } = require('express-suite'); //Initialize the App const app = express(); //bodyParser Middleware app.use( bodyParser.json({ limit: '5mb', extended: true, }), ); //The emptyInputCheck Middleware //It's important that we place the emptyInputCheck Middleware after the bodyParser middleware //Make sure you call the emptyInputCheck middleware before the routes are loaded app.use( emptyInputCheck({ checkGet: true, //Wether to check the GET requests or not emptyBodyMsg: 'Err Msg 1', //Custom msg for empty body emptyFieldMsg: 'Err Msg 2', //Custom msg for missing fields supressFieldKey: false, //To supress the key of the missing field }), ); //Load Routes const example = require('./routes/example'); //Use Routes app.use('/', example); //Start the server app.listen(5003);

Tests

The poject uses Jest for testing.

Clone the Github repo. first. Install the dependencies then run npm test

$ git clone https://github.com/algo7/express-suite.git $ cd express-suite $ npm i $ npm test

Depedency Graph

DepGraph

Generated using dependency-cruiser

Known Issues

express-suite's emptyInputCheck function does not work well with Multer

License

Apache License 2.0

About

A collection of Middlewares for Express

Resources

License

Stars

Watchers

Forks

Packages

No packages published