If you’d like to start a new Express app, there are thousands of articles on how to do so. Most of them, targeting beginners, include all the code in one or two files. Although, technically correct, these articles are quite lax on good practices. In no particular order, I’d like to address the address a couple of them.
App Start Up Issues
The API/web endpoint accepts requests before a database connection was established. Usually, the offending code would be structured like this:
Because, in node.js, almost all the calls are asynchronously, your app will start listening for connections before the database connection was established. Although this might not be a problem when developing the app, it’s a different story when deployed in production. A fast start up time might be 300-500 ms, connecting to redis and mongodb, might take 600 ms or more. Are you sure there are no incoming connections in those 300-600 ms your app takes to start?
A minimal fix, would delay the listening for connections part until the database connection was established.
No Setup For Connecting to Multiple Databases/Services
Most examples connect to zero
or one database/service.
These are fine, simple examples, however I’d like to make them a bit more complex. First, your router should not require your model. If really what to do that query in your router, at minimum, you should pass in the router and model as a dependency.
If you’re inclined to, you’d have a chance to mock router and/or the model, to test them independently.
If one would like to go further, model and/or the redis client can be further refactored into a store.js file that will handle the databases interaction. For example, return a cached version of the articles, and only if none is found, query the database. Maybe also store the last result for 10 seconds.
To write a unit test for the above code, one would start with store.js. The npm packages mockgoose and redis-js could be used to setup an in-memory database for mongo and redis.
Using the same technique, one can inject any type of service that requires configuration and setup (eg. RabbitMQ, Push Notifications Services, websockets). If at a later point, one would like to replace redis with memcache, it shouldn’t be a require('redis') in every file.
Tight Code Coupling
Or as seen in almost every online example require the same local or external module in every file.
can be replaced with
The same can be applied to app.js.
And even to config.js. Beside all the connection strings (mongo, redis, etc), one can use this file to assign a release version to the current app. This maybe be useful if one needs to pass in that info to other services, like sentry.io.
Using the presented building blocks, when setting up an app, a start module can be build, so all the services start only after the required connections were established.
It is a good practise that when writing a new service, one should answer the question, how should this new code be tested? Today, all the cool kids use mongo, but tomorrow, they go hipster and switch to FoxPro.