InversifyJS icon indicating copy to clipboard operation
InversifyJS copied to clipboard

inversify-express-utils error w/ no ctrls

Open jonfasking opened this issue 9 years ago • 6 comments

When there are no controllers registered with a container, an error is thrown.

Expected Behavior

No controllers registered should not cause the app to not work.

Current Behavior

No controllers registered = error, 1 controller registered = no error.

Steps to Reproduce (for bugs)

  1. Build a simple express server & container
  2. do not register any controllers with the container, run the app
  3. add a single controller and re-run the app

Context

I'm building an express app that serves up socket.io. There may not be any controllers right now but I want all the functionality of express-utils in project in case I add some in the near future.

Your Environment

  • Version used: 3.5.2
  • Environment name and version (e.g. Chrome 39, node.js 5.4): node 6.9.2
  • Operating System and version (desktop or mobile): macOS sierra 10.12.5

Stack trace

Error: No matching bindings found for serviceIdentifier: Symbol(Controller)
    at _createSubRequests (/Users/jonathan/Source/huntr/pam/node_modules/inversify/lib/planning/planner.js:107:19)
    at Object.plan (/Users/jonathan/Source/huntr/pam/node_modules/inversify/lib/planning/planner.js:126:5)
    at Container._planAndResolve (/Users/jonathan/Source/huntr/pam/node_modules/inversify/lib/container/container.js:228:37)
    at Container._get (/Users/jonathan/Source/huntr/pam/node_modules/inversify/lib/container/container.js:221:44)
    at Container.getAll (/Users/jonathan/Source/huntr/pam/node_modules/inversify/lib/container/container.js:189:21)
    at InversifyExpressServer.registerControllers (/Users/jonathan/Source/huntr/pam/node_modules/inversify-express-utils/lib/server.js:63:43)
    at InversifyExpressServer.build (/Users/jonathan/Source/huntr/pam/node_modules/inversify-express-utils/lib/server.js:54:14)
    at new PamServer (/Users/jonathan/Source/huntr/pam/src/pam.io8_server.js:28:32)
    at Object.<anonymous> (/Users/jonathan/Source/huntr/pam/index.js:4:14)
    at Module._compile (module.js:570:32)```

jonfasking avatar Jun 30 '17 19:06 jonfasking

I think this is ok. On startup express-utils checks for all controllers in inversify. When it finds them it registered them in express. The reason for the exception is that no controllers are registered and so express-utils cannot register them in express. That´s why I think it's ok because it makes no sense to start a express server without registered routes.

lholznagel avatar Jun 30 '17 19:06 lholznagel

@jonfasking I agree with @lholznagel the error makes sense for me. Why do you want to start the app without controllers?

remojansen avatar Jul 02 '17 18:07 remojansen

Let me just say I really like what you guys are doing with this library. I've been waiting for something like this for a long time.

@remojansen As I stated in my use case, I am using express to serve up a socket.io client that has a great deal of functionality. With express I have more granular control on requests, serving up additional static content, and other things. I want an IOC container & DI under the hood for channels, svcs for those channels, data store interaction, etc. etc. The statement

it makes no sense to start a express server without registered routes.

is just flat out not accurate.

However, I could see where you could make the argument that I could simply use inversify in my particular case if I do not have any controllers yet. If you agree with this statement though, you are stating that this library is ONLY FOR express apps utilizing controllers. If this truly is the case, so be it. However that is not documented anywhere in the readme. On the same note, it does not mean you could gracefully handle something as simple as no controllers = do not invoke registering things that do not exist.

If this was something that you intentionally planned and threw .a specific error, I could live with that. However, you have been told about an error that you're deeming is "OK" because it justifies a rule you're just now adopting. This is not good practice and should not be wrote off as OK.

Either way I'm going to continue using the library for this particular use case and will continue to do so for my other express apps.

-Jon

jonfasking avatar Jul 03 '17 04:07 jonfasking

I understand your point. Yes, by reading your comment I would say use inversify.

Taking a quick look at the readme of inversify-express-utils there are only things for controllers or am I overseeing something where you benefit from express utils?

@remojansen Maybe we have to add something like: These are only utils for express. They support you building controllers with the power of inversify. to the readme to make it clear that these are only utils for creating controllers.

lholznagel avatar Jul 03 '17 06:07 lholznagel

@jonfasking if you are not going to use controllers at all then, using inversify-express-utils might be an overkill. The utils are mostly a router and controller factory. Maybe you will be better off by creating an express app from scratch and adding inversify to your socket process. I have personally never used InversifyJS with sockets but I have no reasons to think that It will not work.

@lholznagel I agree about adding a note in the docs to explain that it is mostly a helper to implement MVC architecture :+1:

remojansen avatar Jul 03 '17 09:07 remojansen

Hi @jonfasking, this issue has now been resolved by https://github.com/inversify/inversify-express-utils/pull/97 and will be available in [email protected]

remojansen avatar Dec 16 '17 13:12 remojansen