GithubHelp home page GithubHelp logo

ryosukecla / express-lazy-router Goto Github PK

View Code? Open in Web Editor NEW

This project forked from azu/express-lazy-router

0.0 1.0 0.0 67 KB

Lazy loading for express router

License: MIT License

JavaScript 0.55% TypeScript 99.45%

express-lazy-router's Introduction

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 for router like React Router, Vue Router.

Also, webpack support experiments.lazyCompilation as experimentally.

So, We can get lazy routing in Node.js Express routing too.

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 babe;
  • complete to launch the express app
  • GET /api/status
  • load api.js
    • Compile api.js by babe;
  • { 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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.