Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
express-lazy-router
Advanced tools
Lazy loading for express router.
I've used ts-node(ts-node-dev) for developing Node.js Web Application. It means that compile all TypeScript files at start time.
Many compilation make startup of the web app slow. Lazy routing avoid this compilation overhead by compiled when needed.
In a frontend, We have already used lazy loading with router like React Router, Vue Router.
Also, webpack support experiments.lazyCompilation as experimentally.
My motivation that We can do lazy routing in Node.js Express routing too.
Results of my project:
Use ts-node-dev + express
Install with npm:
npm install express-lazy-router
import express from 'express';
import { createLazyRouter } from 'express-lazy-router';
const lazyLoad = createLazyRouter({
// In production, Load router asap
preload: process.env.NODE_ENV === 'production',
});
const app = express();
// Load ./path_to_router.js when receive request to "/path_to_router"
app.use(
'/path_to_router',
lazyLoad(() => import('./path_to_router')),
);
app.listen(8000, () => {
console.log(`Example app listening at http://localhost:8000`)
});
preload
Default: false
If it is true
, preload the router module as soon as.
It does not mean sync loading.
Before: No lazy loading
index.js
:
import express from 'express';
import api from "./api";
const app = express();
app.use(
'/api',
api
);
app.listen(8000, () => {
console.log(`Example app listening at http://localhost:8000`)
});
api.js
:
import express from 'express';
const router = express.Router();
// GET api/status
router.get("/status", (_, res) => {
res.json({ ok: true })
});
export default router;
Behavior:
index.js
api.js
GET /api/status
{ ok: true }
After: lazy loading for api.js
index.js
:
import express from 'express';
- import api from "./api";
+ import { createLazyRouter } from 'express-lazy-router';
+ const lazyLoad = createLazyRouter({
+ preload: process.env.NODE_ENV === 'production',
+ });
const app = express();
app.use(
'/api',
- api
+ lazyLoad(() => import("./api"))
);
app.listen(8000, () => {
console.log(`Example app listening at http://localhost:8000`)
});
api.js
: No need to change!
Behavior:
index.js
GET /api/status
api.js
{ ok: true }
The more details behavior when you use loader like @babel/register or ts-node.
index.js
index.js
by babel or ts-nodeGET /api/status
api.js
api.js
by babel or ts-node
{ ok: true }
NG: express-lazy-router does not expect this way.
import { createLazyRouter } from 'express-lazy-router';
const lazyLoad = createLazyRouter();
const app = express();
app.use(lazyLoad(() => import('./path_to_router')));
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
});
See Releases page.
Install devDependencies and Run npm test
:
npm test
Pull requests and stars are always welcome.
For bugs and feature requests, please create an issue.
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
MIT © azu
FAQs
Lazy loading for express router
The npm package express-lazy-router receives a total of 3,021 weekly downloads. As such, express-lazy-router popularity was classified as popular.
We found that express-lazy-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.