Express.JS TypeScript Base
This package provides a few helpers classes that aid in setting up an express server quickly and in an object-orientated manner. This package can also be used in JavaScript.
NodeServer
This class is similar to the default express app.js setup that the express-generator cli will output, however it uses an abstract class instead. In order to use it, you should extend it like so:
It provides a few optional properties and methods that can be implemented in order to customise how the server runs. This is the interface that defines all the optional items (copied from the source):
Running the server
To run the server, simply instantiate it, call the configure
method and then the listen
method. It will deal with the options in the configure
call, and will create the webserver in the listen
call.
.catch;
Controller
This class provides a bit of bootstrapping to allow an object-orientated routing system. Each implementing class should always implement the following method:
protected abstract linkRoutesrouter: Router: void;
Then, when using the controller as a route, you can do the following in the AppServer.mainConfigure
method:
this.express.use'/api', new ApiController.router;
The ApiController
class can also specify sub-routes in the exact same manner, allowing fully nested routes with a clear hierarchy and encapsulated logic. The method of instantiation is entirely optional as well, you could instead use dependency injection and have the sub-controllers be injected already constructed into the controller, and then just call .router()
in the linkRoutes
method. This would allow the controllers to also have services be injected if you needed.