{"id":17766560,"url":"https://github.com/floatdrop/express-dinja","last_synced_at":"2025-03-15T13:30:50.459Z","repository":{"id":15563532,"uuid":"18298785","full_name":"floatdrop/express-dinja","owner":"floatdrop","description":"Dependency injection for Express applications","archived":false,"fork":false,"pushed_at":"2017-08-18T18:35:38.000Z","size":44,"stargazers_count":59,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-14T01:31:43.542Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/floatdrop.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-31T15:51:43.000Z","updated_at":"2024-07-11T12:07:13.000Z","dependencies_parsed_at":"2022-09-24T03:12:52.182Z","dependency_job_id":null,"html_url":"https://github.com/floatdrop/express-dinja","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatdrop%2Fexpress-dinja","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatdrop%2Fexpress-dinja/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatdrop%2Fexpress-dinja/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatdrop%2Fexpress-dinja/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/floatdrop","download_url":"https://codeload.github.com/floatdrop/express-dinja/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243735797,"owners_count":20339530,"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-26T20:32:25.833Z","updated_at":"2025-03-15T13:30:50.448Z","avatar_url":"https://github.com/floatdrop.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# express-dinja\n\n[![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url]\n\nUse dependency injection pattern for Express 4.x applications. Inspired by [express-di](https://github.com/luin/express-di).\n\n## Usage\n\n```js\nvar express = require('express');\nvar app = express();\nvar inject = require('express-dinja')(app);\n\ninject('injected', function (req, res, next) {\n    next(null, 'injected');\n});\n\ninject('dependency', function (injected, req, res, next) {\n    next(null, 'dependency ' + injected);\n});\n\napp.get('/', function (dependency, req, res) {\n    res.json({\n        dinja: dependency\n    });\n});\n\nrequire('http').createServer(app).listen(8080);\n```\n\nOn [localhost:8080](http://localhost:8080) you should see:\n\n```json\n{\n    \"dinja\": \"dependency injected\"\n}\n```\n\n## Why\n\nSuppose you have this graph of middleware dependencies:\n\n![middlewares](https://cloud.githubusercontent.com/assets/365089/2589017/c0292b1a-ba45-11e3-9a1b-57e63d5cdcd2.png)\n\n\nIn express there is no built-in way to start execution of middlewares parallel, so you have two choices:\n\n 1. Linearize middlewares tree and launch them one after one \u0026mdash; and drop performance of app\n 2. Write meta-middleware, that will launch independent middlewares side-by-side \u0026mdash; and write boilerplate code\n\nTo reduce boilerplate code (you can see it in statusTodos function below) dependency injection pattern was added to express route function.\nHere is example how would applications look in plain express and with express-dinja:\n\n![plain-express-vs-dinja](https://cloud.githubusercontent.com/assets/365089/4331364/d3630bca-3fc3-11e4-98cf-4e3ab8b1e6da.png)\n\n## Difference from express-di\n\nThis module is heavily based on code from `express-di`, but has additional features, that I think is necessary for full dependency injection.\n\n * Dependency resolving in injected dependencies\n * No `express.Route.prototype` patching\n * No hardcoded cache\n * No dependency inheritance in mounted apps (todo)\n\n## Benchmark\n\nI used benchmark from [`express-di`](https://github.com/luin/express-di/tree/master/benchmarks) to compare bare express application performance with patched version. Benchmark takes application with one middleware, that uses dependency injection and after middleware is done - sends 'Hello world' response.\n\nMiddleware is faked database connection, which will response in predefined time (horizontal bar) and requests/sec is the vertical bar:\n\n![Performance chart](https://cloud.githubusercontent.com/assets/365089/2590257/9d323e76-ba59-11e3-8ea9-66bae5854c46.png)\n\n## API\n\n### dinja(app)\n\nReturns `Function`, that can inject dependencies into express application `app`. It will patch `route` method to enable injections in route-specific methods like `use`, `get`, `post` and etc.\n\n### inject(name, fn)\n\nInjects dependency with name `name` and dependent express middlewares `fn`.\n\n`fn` almost identically inherits express middleware signature: `function([dependencies], req, res, next)` but with two differences:\n\n 1. It can have own dependencies, that will be resolved, when this middleware is called.\n 2. `next(err, value)` function accepts two arguments: `err` and `value` of dependency.\n\n`req`, `res` and `next` names are pre-defined to corresponding arguments of express middleware.\n\n### inject.resolve(name, callback)\n\nResolves dependency and calls callback:\n\n```js\nfunction resolve(name, cb) {\n    var resolved = this.dependencies[name];\n    if (!resolved) {\n        return cb(new Error('Unknown dependency: ' + name));\n    }\n    return cb(null, resolved);\n}\n```\n\nYou can override it to add caching.\n\n### inject.declare(name, fn)\n\nStores dependency in `inject.dependencies` object:\n\n```js\nfunction declare(name, fn) {\n    this.dependencies[name] = fn;\n}\n```\n\nIf you want to use shared singleton as storage, you can override this (do not forget to override `inject.resolve` as well).\n\n\n## License\n\nThe MIT License (MIT) © [Vsevolod Strukchinsky](mailto:floatdrop@gmail.com)\n\n[travis-url]: https://travis-ci.org/floatdrop/express-dinja\n[travis-image]: http://img.shields.io/travis/floatdrop/express-dinja.svg?style=flat\n\n[coveralls-url]: https://coveralls.io/r/floatdrop/express-dinja\n[coveralls-image]: http://img.shields.io/coveralls/floatdrop/express-dinja.svg?style=flat\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffloatdrop%2Fexpress-dinja","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffloatdrop%2Fexpress-dinja","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffloatdrop%2Fexpress-dinja/lists"}