1.1.3 • Published 7 years ago

express-use-shortcut v1.1.3

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

express-use-shortcut

DEPRECATED: use express-shortcut instead. Helper function that avoids calling app.use multiple times for middlewares. You can pass as many middlewares you need passing Express instance only once.

Installation

npm install express-use-shortcut --save

The problem

The following code is very common in Express applications. It registers many middlewares:

const express = require('express')
    , app = express()
    , cookieParser = require('cookie-parser')
    , session = require('express-session')
    , passport = require('passport');

app.use(cookieParser());
app.use(session(
	{ secret: 'homem avestruz', 
	  resave: true, 
	  saveUninitialized: true 
	}
));
app.use(passport.initialize());
app.use(passport.session());

It's not too hard to figure out that we are calling app.use too many times.

express-use-shortcut usage

Let's see the previous example with express-use-shortcut:

const express = require('express')
    , app = express()
    , cookieParser = require('cookie-parser')
    , session = require('express-session')
    , passport = require('passport')
    , use = require('express-use-shortcut');

use(
    cookieParser(),
    session(
        { secret: 'homem avestruz', 
          resave: true, 
          saveUninitialized: true 
        }
    ),
    passport.session(),
)(app);

If a path is necessary, you can pass the path and the middleware within an array:

use(
    ['/api', yourMiddleware],
    cookieParser(),
    session(
        { secret: 'homem avestruz', 
          resave: true, 
          saveUninitialized: true 
        }
    ),
    passport.session()
)(app);