{"id":15652059,"url":"https://github.com/azu/express-lazy-router","last_synced_at":"2025-04-04T10:05:01.843Z","repository":{"id":52572536,"uuid":"332654941","full_name":"azu/express-lazy-router","owner":"azu","description":"Lazy loading for express router","archived":false,"fork":false,"pushed_at":"2025-03-01T07:46:41.000Z","size":91,"stargazers_count":31,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-28T09:03:58.961Z","etag":null,"topics":["express","lazy","nodejs","performance","ts-node-dev"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/azu.png","metadata":{"funding":{"github":"azu"},"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-01-25T06:43:32.000Z","updated_at":"2025-03-01T07:46:45.000Z","dependencies_parsed_at":"2024-06-18T18:36:55.611Z","dependency_job_id":"955f06ef-5a40-4bfb-a5b8-5274280499d3","html_url":"https://github.com/azu/express-lazy-router","commit_stats":{"total_commits":42,"total_committers":3,"mean_commits":14.0,"dds":"0.19047619047619047","last_synced_commit":"24c6a48a12f66be71dba280b8aaa4284b75ece1b"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azu%2Fexpress-lazy-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azu%2Fexpress-lazy-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azu%2Fexpress-lazy-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azu%2Fexpress-lazy-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/azu","download_url":"https://codeload.github.com/azu/express-lazy-router/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247154661,"owners_count":20892895,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["express","lazy","nodejs","performance","ts-node-dev"],"created_at":"2024-10-03T12:41:10.767Z","updated_at":"2025-04-04T10:05:01.804Z","avatar_url":"https://github.com/azu.png","language":"TypeScript","funding_links":["https://github.com/sponsors/azu"],"categories":[],"sub_categories":[],"readme":"# express-lazy-router\n\nLazy loading for express router.\n\n## Motivation\n\nI've used [ts-node](https://github.com/TypeStrong/ts-node)([ts-node-dev](https://github.com/wclr/ts-node-dev)) for\ndeveloping Node.js Web Application. It means that compile all TypeScript files at start time.\n\nMany compilation make startup of the web app slow. Lazy routing avoid this compilation overhead by compiled when needed.\n\n- [Compilation is unbelievably slow · Issue #754 · TypeStrong/ts-node](https://github.com/TypeStrong/ts-node/issues/754)\n\nIn a frontend, We have already used lazy loading with router like React Router, Vue Router.\n\n- [Route-based code splitting | React](https://reactjs.org/docs/code-splitting.html#route-based-code-splitting)\n- [Lazy Loading Routes | Vue Router](https://router.vuejs.org/guide/advanced/lazy-loading.html)\n\nAlso, [webpack](https://github.com/webpack/webpack) support [experiments.lazyCompilation](https://github.com/webpack/webpack/releases/tag/v5.17.0) as experimentally.\n\nMy motivation that We can do lazy routing in Node.js [Express routing](https://expressjs.com/en/guide/routing.html) too.\n\nResults of my project:\n\n\u003e Use ts-node-dev + express\n\n- Before:\n  - 123 ts file Compilation\n  - Total startup time: 34236ms\n- After(use express-lazy-router):\n  - 14 ts file Compilation\n  - Total startup time: 14238ms\n- Summary:\n  - Compilation time is 200ms per 1 ts file\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n    npm install express-lazy-router\n\n## Usage\n\n```ts\nimport express from 'express';\nimport { createLazyRouter } from 'express-lazy-router';\nconst lazyLoad = createLazyRouter({\n    // In production, Load router asap\n    preload: process.env.NODE_ENV === 'production',\n});\nconst app = express();\n// Load ./path_to_router.js when receive request to \"/path_to_router\"\napp.use(\n    '/path_to_router',\n    lazyLoad(() =\u003e import('./path_to_router')),\n);\napp.listen(8000, () =\u003e {\n  console.log(`Example app listening at http://localhost:8000`)\n});\n```\n\n## Options\n\n### `preload` \n\n\u003e Default: false\n\nIf it is `true`, preload the router module as soon as.\nIt does not mean sync loading.\n\n## Examples\n\n**Before**: No lazy loading\n\n`index.js`:\n\n```js\nimport express from 'express';\nimport api from \"./api\";\nconst app = express();\napp.use(\n    '/api',\n    api\n);\napp.listen(8000, () =\u003e {\n  console.log(`Example app listening at http://localhost:8000`)\n});\n```\n\n`api.js`:\n\n```js\nimport express from 'express';\nconst router = express.Router();\n// GET api/status\nrouter.get(\"/status\", (_, res) =\u003e {\n    res.json({ ok: true })\n});\nexport default router;\n```\n\nBehavior:\n\n- load `index.js`\n- load `api.js`\n- complete to launch the express app \n- `GET /api/status`\n- \u003e `{ ok: true }`\n\n**After**: lazy loading for api.js\n\n`index.js`:\n\n```diff\nimport express from 'express';\n- import api from \"./api\";\n+ import { createLazyRouter } from 'express-lazy-router';\n+ const lazyLoad = createLazyRouter({\n+     preload: process.env.NODE_ENV === 'production',\n+ });\nconst app = express();\napp.use(\n    '/api',\n-    api\n+    lazyLoad(() =\u003e import(\"./api\"))\n);\napp.listen(8000, () =\u003e {\n    console.log(`Example app listening at http://localhost:8000`)\n});\n```\n\n`api.js`: No need to change!\n\nBehavior:\n\n- load `index.js`\n- complete to launch the express app\n- `GET /api/status`\n- load `api.js`\n- \u003e `{ ok: true }`\n\nThe more details behavior when you use loader like [@babel/register](https://babeljs.io/docs/en/babel-register) or [ts-node](https://github.com/TypeStrong/ts-node).\n\n- load `index.js`\n    - Compile `index.js` by babel or ts-node\n- complete to launch the express app\n- `GET /api/status`\n- load `api.js`\n  - Compile `api.js` by babel or ts-node\n- \u003e `{ ok: true }`\n\n## Limitation\n\n### Avoid to use non-path router\n\nNG: express-lazy-router does not expect this way.\n\n```ts\nimport { createLazyRouter } from 'express-lazy-router';\nconst lazyLoad = createLazyRouter();\nconst app = express();\napp.use(lazyLoad(() =\u003e import('./path_to_router')));\napp.listen(port, () =\u003e {\n    console.log(`Example app listening at http://localhost:${port}`)\n});\n```\n\n## Changelog\n\nSee [Releases page](https://github.com/azu/express-lazy-router/releases).\n\n## Running tests\n\nInstall devDependencies and Run `npm test`:\n\n    npm test\n\n## Contributing\n\nPull requests and stars are always welcome.\n\nFor bugs and feature requests, [please create an issue](https://github.com/azu/express-lazy-router/issues).\n\n1. Fork it!\n2. Create your feature branch: `git checkout -b my-new-feature`\n3. Commit your changes: `git commit -am 'Add some feature'`\n4. Push to the branch: `git push origin my-new-feature`\n5. Submit a pull request :D\n\n## Author\n\n- azu: [GitHub](https://github.com/azu), [Twitter](https://twitter.com/azu_re)\n\n## License\n\nMIT © azu\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazu%2Fexpress-lazy-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fazu%2Fexpress-lazy-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazu%2Fexpress-lazy-router/lists"}