Modern Web Development Using Node.js

Sandip Das
Mar 23 · 7 min read

What is Node.js?

“Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine.” nodejs.org

“Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser” — Wikipedia

All above just simply means that you can run your javascript code in server machine (any os) using node.js.

Why Node.js so popular?

  • Node.js has an event-driven architecture capable of asynchronous / non-blocking I/O
  • Node.js aim to optimize throughput and scalability in web applications with many input/output operations, as well as for real-time Web applications.
  • Node.js package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
  • It’s great to work in “JavaScript Everywhere” approach, in front-end as well as back-end having consistency of using same language is just awesome as it sounds.
  • Node.js have proven benchmark for being fast and high performance against the current competitors.
  • Node.js can be hosted anywhere, be it AWS, GCP, Azure, Heroku and many others.
  • If Node.js developers follow particular framework or particular structure then code readability is good just like any other top programming languages.
  • Huge community support is the true blessing

What are the best use case of Node.js?

  • Real time web applications
  • Back-end Specific Applications (used to interact with different databases and cache ), REST / GraphQL Api
  • Blogs, CMS, Social Applications.
  • Robotics — IoT Devices programming (including handling communication with server)
  • Stand Alone Script — e.g. as CRON scripts or just one time execution scripts

There is a lot more use cases but above are the primary use cases

Detailed survey on use of node.js: https://nodejs.org/en/user-survey-report/ (must check)

What about Limitations ?

  • Node.js is a great solution for building simple, mid-complex or highly complex projects. But, it is not the ideal option when you need to deal with CPU-intensive tasks. Due to incoming request blockage by heavy computations, there is a significant loss in performance. On that account, it is not a fit for long-running calculations.
  • Callback hell — This issue can affect the quality of your javascript code and trigger other declines such as development slowdowns and cost increases. Callback hell is a situation caused by execution of multiple asynchronous operations where myriad nested callbacks end up a callback function

How to start node.js development?

I would highly suggest first get familiar with core node.js modules like path, http, url, querystring, fs, util etc. Click here to see full list of core modules.

After that you might be interested in express.js if you want to develop web applications, if you are looking for better frameworks then you can check: Sails.js, Happy.js, Koa.js, Nest.js, Loopback.io, Meteor.js, MEAN.io, Strapi etc.

If you want to make real-time websocket based application you may want to use: Socket.io, basic websocket

In below article I have given reference for good javascript resources as well as node.js courses links as well:

https://www.linkedin.com/pulse/learning-javascript-sandip-das/

What you need to learn more in sense of long term career in Node.js development ?

First of all you will need to know at least one popular regular database like MongoDb, MySql, PostgreSql etc.

To integrate database use:

MongoDB: Mongoose.js ODM

SQL/PostgreSQL : Sequelizejs ORM

For other database don’t worry if it’s really a well known database you will find the client module in npm.

Learn about scaling applications, clustersing, worker process , forking etc

Just read this detailed post: https://medium.freecodecamp.org/scaling-node-js-applications-8492bd8afadc

You will also need to know node.js process events. Atleast read about “uncaughtException” , “unhandledRejection”, “message” , “process.env”, “exit” ,”process.argv”.

Use PM2 or Forever.js to run production applications.

PM2 is suggested by more senior programmers, here’s the detailed post how can you use PM2 to run production level node.js application:

https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps

https://www.tecmint.com/install-pm2-to-run-nodejs-apps-on-linux-server/

Also using PM2 handling cluster logics are very easy:

https://futurestud.io/tutorials/pm2-cluster-mode-and-zero-downtime-restarts

However if you like to use Forever.js, follow below simple article:

https://www.digitalocean.com/community/tutorials/how-to-host-multiple-node-js-applications-on-a-single-vps-with-nginx-forever-and-crontab

But to handle reboot scenarios using forever.js I personally will suggest use forever.js service module: https://www.npmjs.com/package/forever-service

Also Learn About:

Async/Await:

It’s my most favourite feature, specially architected to eliminate the callback hell issue.

Officially Node.js v8.x onwards async/await is available natively in node.js, before async / await , I was using async npm module to handle callback hell issue.

Let’s see how this async/await looks:

Let’s see how code looks without async/await:

You should start thinking about using async/await if you are already not using it.

Read below two articles to understand async/await very well:

1) https://javascript.info/async-await

2) https://medium.freecodecamp.org/avoiding-the-async-await-hell-c77a0fb71c4c

After you actually used to async/await you will have better understanding of where to use it and where to not use, that’s the most important part.

Stable HTTP/2:

In Node v8 an experimental HTTP/2 module was introduced, but it was bit buggy, now in Node v10 HTTP/2 much stable.

What You can achieve using HTTP/2:

  • Server Push (my favourite)
  • Prioritization
  • Header Compression
  • Multiplexing

A detailed article on HTTP/2 you can read on: https://www.smashingmagazine.com/2016/02/getting-ready-for-http2/

Promise:

“A promise represents the eventual result of an asynchronous operation. It is a placeholder into which the successful result value or reason for failure will materialize.”

Here is simplified explanation: https://spring.io/understanding/javascript-promises

Here is very detailed in-action explanation of promises in node.js: https://medium.com/dev-bits/writing-neat-asynchronous-node-js-code-with-promises-32ed3a4fd098

Error handling:

No matter if you are experience or fresher, expected or unexpected error will occur no matter what, always handle errors and error logging.

Different Types of error handling:

  • Error object
  • Try…catch
  • Throw
  • Call stack
  • Effective function naming
  • Asynchronous paradigms like promise

To read in more depth about this errors, refer to: https://stackify.com/node-js-error-handling/

Learn more on logging libraries like :

  • Node-Loggly
  • Bunyan
  • Winston
  • Morgan

Read this full article: https://www.loggly.com/blog/node-js-libraries-make-sophisticated-logging-simpler/

I personally like this article about Winston: https://www.digitalocean.com/community/tutorials/how-to-use-winston-to-log-node-js-applications

Write Tests Cases:

Don’t forget to write test cases, in long run it will save a lot time plus ensure your code quality.

Use famous test runners like Mocha, Chai, Jest. If you have time please read: https://hackernoon.com/testing-node-js-in-2018-10a04dd77391

I liked writing test Jest, here’s a very good article: http://www.albertgao.xyz/2017/05/24/how-to-test-expressjs-with-jest-and-supertest/

Secure your node.js Application:

With growing popularity, attacks on node.js application increases, and that’s not just from outside, from inside as well (read: https://www.bleepingcomputer.com/news/security/compromised-javascript-package-caught-stealing-npm-credentials/).

This topic is so big and being the person who faced issues in production due to security, I can write a lot about it , it’s itself a big topic, may be I will write my past experiences about it later in some other post, but for now, read below two post to make sure your application is secured from known threats:

https://medium.com/@nodepractices/were-under-attack-23-node-js-security-best-practices-e33c146cb87d

https://blog.risingstack.com/node-js-security-checklist/

Finally I will suggest check: https://github.com/sindresorhus/awesome-nodejs , there you will get to know about most useful node.js modules and other node.js related sources, it’s always get updated and I check weekly.

Use ESLint:

For better productivity, quality code writing consider using ESLint plugin. It’s works excellent with both Atom and VSCode.

Setting up ESLint in VSCode: https://travishorn.com/setting-up-eslint-on-vs-code-with-airbnb-javascript-style-guide-6eb78a535ba6

Setting up ESLint in Atom: https://www.acuriousanimal.com/2016/08/14/configuring-atom-with-eslint.html

Thanks for reading!

If you like this post don’t forget to like and share with others, also if you are already using node.js, let everyone know in comment what features you like most about node.js, which node.js packages you like most, or what use case you have observed that can help others, share your knowledge with others.

I have decided that after my AWS Certification exams are over then I will start writing weekly articles on node.js, node.js use cases with different AWS services and on different javascript related subjects.

Past Node.js related articles:

https://www.linkedin.com/pulse/how-use-docker-nodejs-development-sandip-das/

https://www.linkedin.com/pulse/advanced-error-handling-nodejs-best-practices-sandip-das/

https://www.linkedin.com/pulse/deep-dive-nodejs-async-sync-sandip-das/

https://www.linkedin.com/pulse/how-optimize-full-stack-javascript-applications-deal-sandip-das/

https://www.linkedin.com/pulse/cross-platform-desktop-apps-development-nodejs-sandip-das/

https://www.linkedin.com/pulse/why-sailsjs-one-my-favourite-nodejs-web-framework-sandip-das/

https://www.linkedin.com/pulse/how-make-your-nodejs-web-app-faster-sandip-das/

About the Author

Sandip Das is a tech start-up adviser, mentor to young development professionals as well as working for multiple international IT firms , tech-entrepreneurs as Individual IT consultant / Sr. Web Application developer/ Cloud Solution Architect / JavaScript Architect, worked as a team member, helped in development and in making IT decision . His desire is to help both the tech-entrepreneurs & team to help build awesome web based products, make team more knowledgeable, add new Ideas to give WOW expression in product.

More on Sandip here at LinkedIn

Keywords: #nodejs #javascript #webdevelopment

Sandip Das

Written by

Senior Full Stack JavaScript Developer | Cloud Application Architect + Developer (4x AWS Certified) | Tech Advisor

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade