{"action":"created","issue":{"url":"https://api.github.com/repos/focusaurus/express_code_structure/issues/22","repository_url":"https://api.github.com/repos/focusaurus/express_code_structure","labels_url":"https://api.github.com/repos/focusaurus/express_code_structure/issues/22/labels{/name}","comments_url":"https://api.github.com/repos/focusaurus/express_code_structure/issues/22/comments","events_url":"https://api.github.com/repos/focusaurus/express_code_structure/issues/22/events","html_url":"https://github.com/focusaurus/express_code_structure/issues/22","id":205437384,"number":22,"title":"Question: require vs dependency injection?","user":{"login":"tobymurray","id":3683198,"avatar_url":"https://avatars.githubusercontent.com/u/3683198?v=3","gravatar_id":"","url":"https://api.github.com/users/tobymurray","html_url":"https://github.com/tobymurray","followers_url":"https://api.github.com/users/tobymurray/followers","following_url":"https://api.github.com/users/tobymurray/following{/other_user}","gists_url":"https://api.github.com/users/tobymurray/gists{/gist_id}","starred_url":"https://api.github.com/users/tobymurray/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tobymurray/subscriptions","organizations_url":"https://api.github.com/users/tobymurray/orgs","repos_url":"https://api.github.com/users/tobymurray/repos","events_url":"https://api.github.com/users/tobymurray/events{/privacy}","received_events_url":"https://api.github.com/users/tobymurray/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-02-05T16:56:37Z","updated_at":"2017-02-05T17:53:59Z","closed_at":"2017-02-05T17:16:27Z","body":"I'm doing my first Node project, and coming from the Java world. The current project structure has, for example:\r\n```\r\n// index.js\r\n\r\napp.use('/api', require('app/customers/router'))\r\n```\r\nand\r\n```\r\n// users/router.js\r\n\r\nvar router = require('express').Router()\r\nvar needUser = require('./need-user')\r\n\r\nfunction signIn (req, res) {\r\n  /* eslint no-unused-vars:0 */\r\n  // ...\r\n}\r\n\r\nfunction profile (req, res) {\r\n  res.render('users/profile')\r\n}\r\n\r\nrouter.post('/users/sign-in', signIn)\r\nrouter.get('/users/profile', needUser, profile)\r\n\r\nmodule.exports = router\r\n```\r\n\r\nThe repetition of `var router = require('express').Router()` in each of the routers doesn't look particularly appealing to me . What are your thoughts (pros/cons of each method) about going with a more \"dependency injection\" style approach, like the following:\r\n\r\n```\r\n// index.js\r\n\r\nconst router = express.Router();\r\napp.use('/api', require('app/customers/router')(router))\r\n```\r\n\r\n```\r\n// users/router.js\r\n\r\nvar needUser = require('./need-user')\r\n\r\nmodule.exports = function (router) {\r\n  function signIn(req, res) {\r\n    /* eslint no-unused-vars:0 */\r\n    // ...\r\n  }\r\n\r\n  function profile(req, res) {\r\n    res.render('users/profile')\r\n  }\r\n\r\n  router.post('/users/sign-in', signIn)\r\n  router.get('/users/profile', needUser, profile)\r\n\r\n  return router;\r\n}\r\n```\r\n\r\nI think this ends up being functionally the same (again, I'm new, so maybe I'm mistaken) but gets rid of the need to `require` everywhere. Additionally, perhaps not relevant for the router, but for something like a database tool it allows for easier testing as a mock object can be thrown in instead in the test code.\r\n\r\nI'm looking for best practices for my own project, and while it's interesting that ExpressJS is unopinionated, that leaves a void for terrible project structure to thrive. I agree with a lot of the opinions you've expressed here, so I'm interested to see what you think of the code above. "},"comment":{"url":"https://api.github.com/repos/focusaurus/express_code_structure/issues/comments/277536145","html_url":"https://github.com/focusaurus/express_code_structure/issues/22#issuecomment-277536145","issue_url":"https://api.github.com/repos/focusaurus/express_code_structure/issues/22","id":277536145,"user":{"login":"tobymurray","id":3683198,"avatar_url":"https://avatars.githubusercontent.com/u/3683198?v=3","gravatar_id":"","url":"https://api.github.com/users/tobymurray","html_url":"https://github.com/tobymurray","followers_url":"https://api.github.com/users/tobymurray/followers","following_url":"https://api.github.com/users/tobymurray/following{/other_user}","gists_url":"https://api.github.com/users/tobymurray/gists{/gist_id}","starred_url":"https://api.github.com/users/tobymurray/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tobymurray/subscriptions","organizations_url":"https://api.github.com/users/tobymurray/orgs","repos_url":"https://api.github.com/users/tobymurray/repos","events_url":"https://api.github.com/users/tobymurray/events{/privacy}","received_events_url":"https://api.github.com/users/tobymurray/received_events","type":"User","site_admin":false},"created_at":"2017-02-05T17:53:59Z","updated_at":"2017-02-05T17:53:59Z","body":"> It's not that DI itself doesn't seem useful to me \r\n\r\nI've used [Dagger](https://google.github.io/dagger/) in an enterprise Java application, and I thought it was wonderful. It sucks when the graph gets huge, but having run into enough global singletons it seems far better than the alternatives I'm aware of.\r\n\r\n> I'd invite you to clarify \"doesn't look particularly appealing\" for yourself. Why not?\r\n\r\nBiggest reason in Java world would be testing, although I admit I'm completely unfamiliar with the technologies you mentioned. With the ability to monkey patch, perhaps that's irrelevant in JavaScript - I'll take a look! Secondly though, and I think still relevant here, is as a layer of abstraction between dependencies and the functionality that depends on them. The router is probably a terrible example for me to use, but imagining I wanted to use a different router it'd suck to have to go to all the separate files and change them all vs. changing in one place.\r\n\r\n> it's just that if I'm going to use it, I want it baked into the programming language itself where it belongs, not tacked on with a hacky userland system.\r\n\r\nWhat do you mean by a \"hacky userland system\"? Isn't it just how `require` works - you can provide arguments to it? Are you referring to where the dependencies are initialized, and how there's no tooling to support or enforce them/actually do the \"injection\" part of the DI? Is it meaningfully different from the `app.use` functionality for middleware?\r\n\r\n> Now I prefer modular subapps with their own router for cleaner isolation.\r\n\r\nIs the sentiment here something along the lines of you being able to take a subdirectory out and drop it in its own application and run more effectively \"as is\"? What's the isolation in this case refer to - does it make sense for every object that adds a route to create its own router? Isn't that not respecting a separation of concerns?\r\n\r\n> Having worked with AngularJS's disasterous DI system\r\n\r\nIs that Angular 1? I've used Angular 2 and it seems pretty straightforward and usable, although I've only used it on smaller applications. \r\n\r\n--- \r\nCompletely separately, how do you share resources that require initialization - e.g. a database connection  pool with something like [Knex](http://knexjs.org/)) if you need access in all of the model files like `need-user.js`. I don't see an example of that in this project structure - if you have some insight, I'd be up for doing up a PR to incorporate an example into this project.\r\n\r\nThanks so much for your response! Great to get an opinion from someone who's been immersed in the ecosystem for a long time. "}}
{"actor":{"display_login":"tobymurray"}}