express-middleware-loader
v1.0.4
Published
Loads files in middleware folder as middlewares automatically
Downloads
3
Maintainers
Readme
If you're tired of buch of code in your index.js file with a lot of middlewares and you want to seperate every middleware to autoloaded file.
Installation
npm install --save express-middleware-loader
Useage
Create folder where all your middleware files will be (default name is middlewares
) and in your main file (propably index.js
) use
// myapp/index.js
var app = express(); //express app object
require("express-middleware-loader")(app, "middlewares");
Where app
is express app object and middlewares
is folder of middlewares name (optional)
Then each file in middlewares
file can be like:
// myapp/middlewares/myMiddleware.js
module.exports = function(req, res, next) {
// some code
next();
}
or like (example with external middlewares like bodyParser)
// myapp/middlewares/bodyParser.js
var bodyParser = require('body-parser');
module.exports = bodyParser.json();
You can also define multiple middlewares in one file by creating array:
// myapp/middlewares/bodyParser.js
var bodyParser = require('body-parser');
module.exports = [
bodyParser.json(),
bodyParser.urlencoded({extended: true})
];