express-lazy-middleware
Installation
$ npm install --save express-lazy-middleware
# Or with yarn
$ yarn add express-lazy-middleware
Usage
Basic usage:
'use strict';
/** Imports */
import * as Express from 'express';
import lazy from 'express-lazy-middleware';
/** Init */
app.get('/users', lazy(() => (req, res, next) => {
res
.send('I am too lazy');
}));
/** Start */
app.listen(3000);
Lazy require a module:
'use strict';
/** Imports */
import * as Express from 'express';
import lazy from 'express-lazy-middleware';
/** Init */
// `require` will be called after request to the `/users`.
// Note: when you use a ES6 module with `require()` don't forget about `default`
// export.
app.use('/users', lazy(() => require('./users').default));
/** Start */
app.listen(3000);
Init with promise:
'use strict';
/** Imports */
import * as Express from 'express';
import lazy from 'express-lazy-middleware';
/** Init */
// The `loader` (first argument) function will be called only once.
app.get('/session', lazy(async () => {
await new Promise((resolve, reject) => {
setTimeout(resolve, 5);
});
return require('./session').get;
}));
/** Start */
app.listen(3000);
Build
$ npm install
$ # or
$ yarn
$
$ npm run build
Test
$ npm run test
Contributing
- Fork it (https://github.com/SuperPaintman/express-lazy-middleware/fork)
- Create your feature branch (
git checkout -b feature/<feature_name>
) - Commit your changes (
git commit -am '<type>(<scope>): added some feature'
) - Push to the branch (
git push origin feature/<feature_name>
) - Create a new Pull Request
Contributors
- SuperPaintman SuperPaintman - creator, maintainer