Like any kind of apps, JavaScript apps also have to be written well.
Otherwise, we run into all kinds of issues later on.
In this article, we’ll look at some best practices we should follow when writing Node apps.
Inspect for Outdated Packages
We can inspect for outdated packages with the npm outdated
and npm-check-updates
tools.
They detect installed packages which are outdated.
To automate this, we can inject this into the CI pipeline to check during build time.
This way, we’ll update the packages as soon as possible.
Use Production-Like Environment for End to End Testing
We should test our app in a production-like environment to simulate real user interaction.
This is the whole point of end to end tests.
We run the tests in a clean, production-like environment so that we know what will happen if the app goes to production.
Also, we should reset the database to the original seed data after each test so that all tests run independently in isolation.
We can use docker-compose to reset our database when we spin up our environment with it.
Refactor Regularly Using Static Analysis Tools
We can use static analysis tools to detect things that aren’t picked up by linters.
Issues like duplicates and code complexity can only be picked by static analysis tools.
Therefore, we’ve to use them to help us check for those and we can fix them one by one.
Some tools to do the checks include Sonarqube and Code Climate.
Carefully Choose your CI Platform
We need some a CI platform to run all our tests and build our app so we can deploy them easily.
They should make our lives easier.
There are many choices like Jenkins and CircleCI.
We’ve to choose one that has the features we need and have good speed.
Test Middlewares in Isolation
Middlewares should be tested in isolation so that we can catch any bugs earlier.
Since they’re used in many places, any bugs with them will affect many endpoints.
We can stub and spy on the request, response and next
function to do our tests.
Monitoring
Monitoring our app is definitely something that should be done all the time.
If there’re problems like when the app goes down, we must know right away.
Otherwise, we’ll have many angry and frustrated users.
They have many graphs and tables to show us what’s going with our app.
Increase Transparency Using Smart Logging
Without logging, our app is a black box.
We wouldn’t know what our app is doing without logs.
Therefore, we need to find a logging solution so that we can easily search and extract log activities.
We need a reputable logging library like Winston to do our logging.
And we need to put in a location that we can visualize and search for the data easily.
The error tracking tools should also give us useful metrics like error rates that we can check.
Conclusion
We should check for outdated packages and automate our builds.
Also, monitoring and smart logging are essential.