Inserve
Inserve aims to extend Express with a number of features that are vital while working on large codebases at scale. The goal is that it remains a very thin wrapper layer around Express, without significant divergence.
Features
- First-class support for dependency injection into route handlers (using Microsoft's tsyringe library)
- A really great TypeScript experience. Typings built-in to the package, and used from the start, not as an afterthought.
- Compatibility with everything else that is made for Express. We don't change the fundamentals at all.
- BYOE: "Bring your own Express." We depend on Express and tsyringe as peer dependencies. The versions used in your project will be used by Inserve also.
Example #1 - Hello World
;;; // Define a route handler class. These can be used// as middleware, endpoints, etc. // Create and start the server;server.get'/', HelloWorld;server.listen8080;
Pretty simple. But, this code is longer than the equivalent code in Express! So surely, there is a useful example, right? Right, that's where dependency injection comes in!
Example #2 - Dependency Injection (w/ tsyringe)
;;;; /** * This injection token can be used to inject / resolve a database implementation */; /** * Interface, which defines behaviors but not the specifics of how they are performed */ /** * Implementation, which defined implementation details for the aforementioned behaviors */ // Create the server instance;server.get'/', RequireAccessToken, HelloWorld; // Inject our database implementationserver.getContainer.registerDATABASE_TOKEN, MyDatabase; // Start the databaseserver.listen8080;
Conclusion
For large codebases, where it's crucial to build generic components which can be used in multiple different environments, dependency injection is a must.
Inserve enables your Express servers to take advantage of dependency injection. Routes can define what services and values they need, and the upstream provider / environment can inject the appropriate implementation at runtime.
Hopefully you find this useful. I intend to add to this as needed for my other projects that depend on it; but, if you think of something you need, I an open to adding it too.