Node.js tips and tricks

InfoWorld | Apr 16, 2018

In this explainer video, learn several techniques that can improve your Node development experience.

Similar
Welcome to Node J S tips and tricks
Node J S is a cross-platform JavaScript runtime environment that is useful for both servers and desktop applications.
Node is a platform built on Chrome's JavaScript runtime, for easily building fast, scalable network applications. It uses an event-driven, non-blocking I O model that makes it lightweight and efficient, and perfect for data-intensive real-time applications that run across distributed devices.
Node offers many benefits, including low memory usage, good performance, and a large ecosystem of packages, currently numbering about four hundred and seventy-five thousand.
Today we’ll discuss several techniques that can improve your Node development experience.
To avoid reinventing the wheel, use existing N P M packages when they are applicable to your project.

Unfortunately, the standard site for searching N P M packages doesn’t work very well. Instead, use N P M S dot I O to screen packages for security, stability, quality, and current maintenance.
There are many curated lists of N P M packages and resources. One good list, awesome dash node j s, can be reached by browsing to node dot cool.
Simple code is less likely to have errors and more likely to be maintainable than complex code. There are no points for being clever. Some popular and useful n p m modules consist of three lines of code.
Unless you are writing CLI commands, you probably want your app to be responsive while performing longer-running operations such as reads and writes, so you should choose asynchronous code over synchronous as much as possible. After all, most Node apps are web server applications, which are inherently multi-user.
Put all your require statements at the top of the file because they are synchronous and will block the code’s execution. It’s better to block once on load than block the program randomly while it’s running.

Node Security Platform (node security dot I O) and Snyk (snick dot I O) can help you check that your code is secure and that your open source modules don’t have known vulnerabilities.
Run your production servers like cattle, not like pets. If a server goes down, you should have a process manager in place to kill it and start a new instance. Common examples include the StrongLoop process manager, P M Two, and the Forever and forever-monitor N P M packages.
The needs of a development server differ from the needs of a production server. Use nodemon (node'mon) instead of node to run your code, and your process will automatically restart when your code changes.
It’s hard to catch errors in asynchronous code unless you check every error return value in every callback. The resulting messy code is sometimes called callback hell. The N P M okay package allows you to easily bubble errors up your callback chain.
Thank you for joining us today for these 10 Node J S tips and tricks.
Popular