- Node.js Best Practices – How to Become a Better Node.js Developer in 2018
- By GERGELY NEMETH
- The Nuggets translation Project
- Permanent link to this article: github.com/xitu/gold-m…
- Translator: Yong Li
- Proofreader: Guoyang Moods445
Node.js Best Practices — How to Become a Better Node.js developer in 2018
Over the past two years, it has become a tradition for me to write a piece of advice every year on how to become a better Node.js developer in the coming year. This year is no exception! 🤗
If you’re interested in my previous New Year’s advice, you can read it on the RisingStack blog:
- How to become a Better Node.js developer in 2016
- How to Be a Better developer in 2017
Without further ado, here are some suggestions for 2018!
usingasync-await
With the release of Node.js 8, async functions are widely available. With async functions, you can replace callbacks and write asynchronous code that reads just like synchronous code.
But what is async function? Let’s review the best practices of Node.js Async functions:
The async function lets you write code that reads like synchronous code, but is actually based on Promise. Once you have used the async keyword to define a function, you can use the await keyword inside the function. When async is called, it returns a Promise. This Promise is fulfilled when the async function body returns a value. This Promise fails when the async function throws an error.
The await keyword can be used to wait for a Promise to complete and return the result value. If the value passed to the await keyword is not a Promise, it converts it to a completed Promise.
If you want to master async functions, I recommend browsing these resources:
- Learning to throw again
- Catching without awating
- Node.js async function best practices
Let your application terminate gracefully
When you deploy a new version of your application, you must replace the old version. The process manager you’re using, whether it’s Heroku, Kubernetes, Supervisor, or any other process manager, first sends the application a SIGTERM signal to inform it that it’s about to be aborted. Once the application gets the signal, it should stop accepting new requests, complete all pending requests, and clean up the resources it uses. Resources typically contain database connections and file locks.
To make this process easier, we’ve released an open source module called Terminus on GoDaddy to help you gracefully abort your application. Check out ☺️ now
Adopt the same style guide throughout the company
Adopting a style guide in a company with a hundred-person development team can be challenging — getting everyone to agree on the same set of rules is impossible.
With all due respect: you can never get hundreds of developers to agree on the same set of principles, even if there are obvious benefits, such as allowing teams to switch from project to project more quickly, rather than spending time and effort getting used to a new (if slightly different) coding style.
If you’re working in a team environment like this, I’ve found it’s best to trust an experienced programmer and work with others to decide what guidelines to include in the style guide, but he has the final say. Until everyone can follow the same set of rules, it doesn’t matter what it says (I don’t want to start a row about semicolons). What matters is that a decision must be made at some point.
Make safety a must
We are seeing more and more companies listed on HaveiBeenpwned – I bet you don’t want to be next. When you release a piece of new code to your users, the code review should include security experts. If you don’t have that talent in your company, or if they’re very, very busy, a good solution is to partner with a company like Lift Security.
You, as a developer, should also strive to update your security knowledge. To that end, I recommend reading these materials:
- Node.js security checklist
- The Open Web Application Security Project website
- Snyk blog
Speak at meetings and conferences
Another way to become a better developer, and even better at expressing yourself, is to speak at meet-and-greets and conferences. If you’ve never tried this before, I recommend starting with a local meet-and-greet and then trying to apply for national or international conferences.
I understand that public speaking can be difficult. Speaking. IO helped me a lot when I was preparing my first presentation, and I recommend you check it out. If you’re working on your first presentation and want some feedback, you can talk to me on Twitter and I’d be happy to help!
Once you have a topic you’d like to share at the conference, you can check out the 2018 Web Conference Collection on Github, which is awesome!
Write modules directly using the new browser API
Mikeal had a great post on Modern Modules back in September. One of my favorite things to do is to use browser apis to write modules that polyfill Node.js when necessary. The obvious advantage of this is that you can publish smaller JavaScript code into the browser (and make pages load faster). On the other hand, no one cares if your back-end dependencies are too onerous.
Adopt the 12-factors rule for application development
The 12-factors principle of application development, which describes best practices for how web applications should be written, is also on my list of suggestions this year.
With the increasing use of Kubernetes and other orchestration engines, it is becoming increasingly important to follow the 12-factors rule. They cover the following areas:
- One base code, multiple deployments
- Display declaration and separation dependencies
- Store the configuration in the environment
- Think of backend services as additional resources
- Strictly separate build and run
- Run the application as one or more stateless processes
- Services are provided through port binding
- Extend through the process model
- Fast start and graceful abort maximizes robustness
- Keep development, pre-release, and online environments as identical as possible
- Think of logs as a stream of events
- Background administrative tasks run as one-off processes
Learn new ECMASCript features
Some new ECMAScript features can dramatically improve your productivity. They can help you write clear code that speaks for itself. Some of my favorite features (which are not very new, to be honest) are:
- Extended syntax
- The remaining parameters
- deconstruction
- Async function
If you want a complete look at the new ECMAScript features, I recommend reading the book ES6 & Beyond.
What else would you like to add to this list? Let me know in the comments.
The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.