Node.js Best Practices for Beginners and Advanced Users Alike

Sagar Agrawal
JavaScript in Plain English
5 min readOct 15, 2021

--

Image of a computer with some code on it

Learning to write code is easy. Learning to write high-quality code is where things start to get complicated even if you are a mid-level developer.

While there is no one size fit when it comes to the guideline for high-quality code, I have tried to cover many of the tips/guidelines that are great in my opinion.

This article gives you an overview of knowledge on how to write high-quality code in Node.js. There will be many more articles in the future which will cover each sub-topic in detail if you want to know more about them.

Life skills

  • Properly reading the documentation of technologies you are using
  • Have a mentor or people who have a higher knowledge of Node.js so you can get advice/help from them.
  • Improve communications
  • Attend meetups/conference/webinars monthly
  • Read articles at least weekly
  • Check for a similar open-source product for inspiration

Programming's languages, Concepts & Tools

  • Data structure & Algorithms
  • GIT
  • Immutability
  • Event-driven system
  • JavaScript
  • Node.js
  • Docker and Kubernetes
  • VS Code (plugins, snippets, shortcut keys, command)
  • Clouds like AWS (especially EC2, S3, Lambda)
  • API / REST
  • Different design patterns
  • Webpack
  • Cypress | Testing
  • Jira | Project Management

Planning

  • Know your requirements
  • Know and predict your future requirements
  • Prepare user flows and major features documentation
  • Have a task and timeline in task/project management system (like Jira)
  • The balance between libraries to use and writing custom code (Express, Loadash, etc)

Folder Structure

The folder structure would also need to change based on the project requirements, generally, we can use two of the below structures,

This can be used for large projects

This can be used for small projects

Image of recommended folder structure for node.js

Code Quality, Documentation, Error handling & Logging

  • Properly comment everything
  • Linting and follow a guideline like| Airbnb | Google | Custom
  • Follow ES6+ code guidelines
  • User logging libraries like Winston instead of console.log
  • Using swagger for API documentation
  • Using JSdoc for code documentation
  • Proper response and request structure through API’s

Execution/Deployment

As the developer roles become more and more fluid, you must know at least the basics of cloud, servers, and deployment-related things, so you can handle your own weight for most things

  • Use cloud like AWS
  • Set up CI or CI/CD
  • Steps about the build, start, stop and test in Readme file
  • Use process manager like PM2 for auto-restart during deployment
  • Use process manager like Nodemon for hot reloading
  • Monitor your application using APM
  • Use environment variables to store credentials and other data’s

Testing

While there are tons of different ways and different things you can test, we should generally focus on the test that matters depending upon the project and requirement.

  • Unit testing
  • E2E / API testing using postman or cypress
  • Dependency injection design concept
  • Test Coverage
  • Use test libraries like faker.js

Performance

  • Delegate Gzip, and things like SSL to reverse proxy, if possible, like Nginx
  • Serving frontend assets from S3, CDN or reverse proxy rather than node
  • Use caching mechanisms like Redis
  • Use queuing mechanism with Redis or RabbitMQ
  • Try to avoid using CPU intensive tasks in Node.js
  • Use proper Data structure and efficient algorithms
  • Implement concepts like paginations and sent only whatever is needed
  • Use better infrastructure for your program to run
  • Embrace and learn more about the async
  • Run multiple Node instances
  • Embrace microservices architecture
  • Use proper frameworks and avoid heavy libraries
  • Write efficient database queries
  • Learn more about Node.js worker threads
  • Learn more about Node.js clustering

Security

Even doing at least doing basic security measures could help your application a lot.

  • Packages checking (snyl.io)
  • Store creds in an environment variable and not in code
  • CORS and CSRF
  • Helmet package
  • Input and display validation (forms, escaping HTML, JavaScript)
  • Security testing
  • Closing ports
  • Rate limiting and limited attempts for login
  • Encryption for sensitive data like passwords, health data, etc using secure libraries like crypto
  • Use services like Cloudflare for DDOS
  • Keeping Cookie secure
  • Ability to block JWT using services like Redis

Debugging

Sometimes finding the issues takes more time than rewriting the code if you don’t follow the proper systematic way to debug it.

  • Learn Node debugger
  • Learn postman
  • Learn chrome inspect elements
  • Use APM like a sentry

Working Smart

  • Setup snippets for repeated and most used code
  • Don’t reinvent the wheel and use node libraries to save time and cost
  • Setup most needed/popular VSCode extensions like bracket pair colorizer
  • Setup path alias to make code more robust/easier/cleaner

Common mistakes to avoid

  • Avoid callbacks as much as possible (Callback hell)
  • Not applying system and process to learn all the above things
  • Writing the code from day 1 instead of planning properly
  • Not asking for help/advice/review from senior engineers
  • Reading the above article only once instead of reading it like twice every week for like 2–3 weeks
  • Not learning even, a little bit of frontend
  • Not reading articles weekly to stay updated
  • Not using intelligent paths
  • Thinking I will write test’s once I complete the project, HAHAHAHAHAHA

If you try to go through all this in a single go can be overwhelming, boring, and tough. Try to cover each sub-topic per day so you can properly absorb all the things.

While there are a lot of other tips and guidelines which are needed to maintain the high quality of code (could be different and contradicting to this article), the above overview is a good start that can make you stand out from the crowd and transition to a better developer.

Do mind, even though there are a lot of small things to care of, almost everything has a low learning curve and can be learned with proper effort.

Do let us know if this article helped you in any way or if you think something is missing so that we can improve and help others better.

We wish you best of the luck in your developer journey.

Happy coding, cheers!

More content at plainenglish.io

--

--

Software engineer with entrepreneurial experience, passionate about building solutions, and providing relevant, accurate and actionable insights.