{"id":16329973,"url":"https://github.com/luin/express-di","last_synced_at":"2025-07-14T06:34:55.090Z","repository":{"id":14134392,"uuid":"16839834","full_name":"luin/express-di","owner":"luin","description":"Dependency injection for Express applications","archived":false,"fork":false,"pushed_at":"2017-01-25T05:13:13.000Z","size":43,"stargazers_count":160,"open_issues_count":2,"forks_count":8,"subscribers_count":6,"default_branch":"4.0","last_synced_at":"2025-06-12T08:43:03.989Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/luin.png","metadata":{"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}},"created_at":"2014-02-14T14:47:01.000Z","updated_at":"2025-02-16T04:20:38.000Z","dependencies_parsed_at":"2022-08-26T17:42:04.286Z","dependency_job_id":null,"html_url":"https://github.com/luin/express-di","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/luin/express-di","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luin%2Fexpress-di","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luin%2Fexpress-di/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luin%2Fexpress-di/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luin%2Fexpress-di/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luin","download_url":"https://codeload.github.com/luin/express-di/tar.gz/refs/heads/4.0","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luin%2Fexpress-di/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265009443,"owners_count":23697177,"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":[],"created_at":"2024-10-10T23:17:57.209Z","updated_at":"2025-07-14T06:34:55.057Z","avatar_url":"https://github.com/luin.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"express-di\n==========\n[![Build Status](https://travis-ci.org/luin/express-di.png?branch=master)](https://travis-ci.org/luin/express-di)\n[![Code Climate](https://codeclimate.com/github/luin/express-di.png)](https://codeclimate.com/github/luin/express-di)\n\nInstallation\n-----\n    npm install express-di\n\nCompatibility\n-----\n    Express 3.x use express-di 3.x\n    Express 4.x use express-di 5.x or 4.x\n\nUsage\n-----\nTo get started simply `require('express-di')` before `var app = express()`, and this module will monkey-patch Express, allowing you to define \"dependencies\" by providing the `app.factory()` method, after which you can use the \"dependencies\" in your routes following the [Dependency Injection pattern(DI)](http://docs.angularjs.org/guide/di).\n\nExample\n-----\nIn the past, if you want to pass variables between middlewares, you have to tack on properties to `req`, which seems odd and uncontrollable(that you couldn't point out easily which middleware add what properties to `req`). For example:\n\n```javascript\nvar express = require('express');\nvar app = express();\n\nvar middleware1 = function(req, res, next) {\n  req.people1 = { name: \"Bob\" };\n  next();\n};\n\nvar middleware2 = function(req, res, next) {\n  req.people2 = { name: \"Jeff\" };\n  next();\n};\n\napp.get('/', middleware1, middleware2, function(req, res) {\n  res.json({\n    people1: req.people1,\n    people2: req.people2\n  });\n});\n\nrequire('http').createServer(app).listen(3008);\n```\n\nAfter using express-di, you can do this:\n\n```javascript\nvar express = require('express');\n// Require express-di\nrequire('express-di');\nvar app = express();\n\napp.factory('people1', function(req, res, next) {\n  next(null, { name: \"Bob\" });\n});\n\napp.factory('people2', function(req, res, next) {\n  next(null, { name: \"Jeff\" });\n});\n\napp.get('/', function(people1, people2, res) {\n  res.json({\n    people1: people1,\n    people2: people2\n  });\n});\n\nrequire('http').createServer(app).listen(3008);\n\n```\n\nDefine a dependency\n-----\nThe `app.factory(name, fn)` method is used to define a dependency.\n\n### Arguments\n\n* `name`: The name of the dependency.\n* `fn`: A function that is like a typical express middleware, takes 3 arguments, `req`, `res` and `next`, with a subtle difference that the `next` function takes 2 arguments: an error(can be null) and the value of the dependency.\n\n### Default dependencies\nexpress-di has defined three default dependencies: `req`, `res` and `next`, so that you can use these arguments in your router middlewares just as before.\n\n\nCache\n-----\nThe same dependency will be cached per request. For instance:\n\n```javascript\napp.factory('me', function(req, res, next) {\n  // This code block will only be executed once per request.\n  User.find(req.params.userId, next);\n});\n\nvar checkPermission = function(me, next) {\n  if (!me) {\n    return next(new Error('No permission.'));\n  }\n  next();\n};\n\napp.get('/me', checkPermission, function(me, res) {\n  res.json(me);\n});\n```\n\nWhere can I use DI?\n-----\nYou can use DI in your route-specific middlewares(aka `app.get()`, `app.post()`, `app.put()`...).\n\nSub App\n-----\nExpress-DI supports sub apps out of the box. Parent app cannot access the dependencies defined in the children apps, while children apps inherits the dependencies defined in the parent app:\n\n    var express = require('express');\n    require('express-di');\n    var mainApp = express();\n    var subApp = express();\n    mainApp.use(subApp);\n\n    mainApp.factory('parents', function(req, res, next) {\n      next(null, 'parents');\n    });\n\n    subApp.factory('children', function(req, res, next) {\n      next(null, 'children');\n    });\n\n    mainApp.get('/parents', function(children, res) {\n      // throws error\n      res.json(children);\n    });\n\n    subApp.get('/children', function(parents, res) {\n      res.json(parents);\n    });\n\nPerformance\n-----\nThe process of DI will only be executed once at startup, so you don't need to worry about the performance.\n\nYou can test the performance using `make bench`.\n\nBenchmark requires [`wrk`](https://github.com/wg/wrk) to be installed first. You can run `brew install wrk` for Mac OS, or [build it](https://github.com/wg/wrk/issues/39) from sources for Ubuntu.\n\nTest\n-----\n* `make test`\n* `make test-cov` will create the coverage.html showing the test-coverage of this module.\n\nArticles and Recipes\n-----\n* [Node Roundup: cipherhub, slate, express-di](http://dailyjs.com/2014/03/19/node-roundup/)\n* [Express 框架 middleware 的依赖问题与解决方案](http://zihua.li/2014/03/using-dependency-injection-to-optimise-express-middlewares/) [Chinese]\n\nLicense\n-----\nThe MIT License (MIT)\n\nCopyright (c) 2014 Zihua Li\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluin%2Fexpress-di","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluin%2Fexpress-di","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluin%2Fexpress-di/lists"}