All Projects → azu → express-lazy-router

azu / express-lazy-router

Licence: MIT license
Lazy loading for express router

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to express-lazy-router

Lazy Rdp
Script for automatic scanning & brute-force RDP
Stars: ✭ 118 (+337.04%)
Mutual labels:  lazy
laravelmanthra
Laravel Crud Generator, I have working for years and I can tell you... It's all CRUD 💩💩💩
Stars: ✭ 27 (+0%)
Mutual labels:  lazy
vue2-data-tree
A tree that data is lazy loaded. Support dragging node, checking node, editing node's name and selecting node.
Stars: ✭ 41 (+51.85%)
Mutual labels:  lazy
Itiriri
A library built for ES6 iteration protocol.
Stars: ✭ 155 (+474.07%)
Mutual labels:  lazy
Lazyblorg
Blogging with Org-mode for very lazy people
Stars: ✭ 226 (+737.04%)
Mutual labels:  lazy
nvim-config
My neovim config
Stars: ✭ 63 (+133.33%)
Mutual labels:  lazy
Lazy Compile Webpack Plugin
Boost webpack startup time by lazily compiling dynamic imports
Stars: ✭ 106 (+292.59%)
Mutual labels:  lazy
lazyjsonmapper
Advanced, intelligent & automatic object-oriented JSON containers for PHP.
Stars: ✭ 48 (+77.78%)
Mutual labels:  lazy
lazy-kit
A new design system for developing with less effort. See how it looks:
Stars: ✭ 68 (+151.85%)
Mutual labels:  lazy
typescript-lazy-get-decorator
Lazily evaluates a getter on an object and caches the returned value
Stars: ✭ 33 (+22.22%)
Mutual labels:  lazy
Hitchcock
The Master of Suspense 🍿
Stars: ✭ 167 (+518.52%)
Mutual labels:  lazy
Vue Cheatsheet
Modified version of the official VueMastery cheatsheet
Stars: ✭ 188 (+596.3%)
Mutual labels:  lazy
react-component-transition
Easy animations between react component transitions.
Stars: ✭ 20 (-25.93%)
Mutual labels:  lazy
Lazy Collections
Collection of fast and lazy operations
Stars: ✭ 146 (+440.74%)
Mutual labels:  lazy
lazyExcel
a simply software like MS-Excel.it can be running muiti-platform...
Stars: ✭ 37 (+37.04%)
Mutual labels:  lazy
Lazyhub
lazyhub - Terminal UI Client for GitHub using gocui.
Stars: ✭ 133 (+392.59%)
Mutual labels:  lazy
lazy-load-images.js
Progressive & lazy loading images.
Stars: ✭ 17 (-37.04%)
Mutual labels:  lazy
lazysodium-java
A Java implementation of the Libsodium crypto library. For the lazy dev.
Stars: ✭ 110 (+307.41%)
Mutual labels:  lazy
blazor-lazy-loading
Automatic Lazy Loading support for Blazor (Server and WebAssembly)
Stars: ✭ 89 (+229.63%)
Mutual labels:  lazy
relaze
Tiny image lazy loading library for React.
Stars: ✭ 20 (-25.93%)
Mutual labels:  lazy

express-lazy-router

Lazy loading for express router.

Motivation

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

  • Before:
    • 123 ts file Compilation
    • Total startup time: 34236ms
  • After(use express-lazy-router):
    • 14 ts file Compilation
    • Total startup time: 14238ms
  • Summary:
    • Compilation time is 200ms per 1 ts file

Install

Install with npm:

npm install express-lazy-router

Usage

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`)
});

Options

preload

Default: false

If it is true, preload the router module as soon as. It does not mean sync loading.

Examples

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:

  • load index.js
  • load api.js
  • complete to launch the express app
  • 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:

  • load index.js
  • complete to launch the express app
  • GET /api/status
  • load api.js
  • { ok: true }

The more details behavior when you use loader like @babel/register or ts-node.

  • load index.js
    • Compile index.js by babel or ts-node
  • complete to launch the express app
  • GET /api/status
  • load api.js
    • Compile api.js by babel or ts-node
  • { ok: true }

Limitation

Avoid to use non-path router

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}`)
});

Changelog

See Releases page.

Running tests

Install devDependencies and Run npm test:

npm test

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

License

MIT © azu

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].