10 NodeJS Tips and Tricks for developers
10 NodeJS Tips and Tricks for developers
Node offers many benefits including Low Memory Usage, Good Performance and Large Ecosystem of Packages currently numbering about 475000.
10 Tips to working in NodeJS
1. Use NPM packages
To avoid reinventing the wheel use existing NPM packages when they are plug-able to your project. Below is the list of top 15 NPM packages used by developers.1. React JS
2. Prop-types
3. Moment
4. Express
5. Request
6. Lodash
7. Async
8. Chalk
9. Commander
11. Debug
12. PM2
13. Mocha
14. Passport
15. Nodemailer
16. Mongoose
2. Use NPMS.IO to find packages
- To search NPM packages use NPMS.IO instead of using NPMJS.COM.
- npms.io is a replacement to the official npmjs.com search but with a key difference: the quality of the results are much much better:
- npmjs.com search is not fine tuned and, because of that, produces strange results. Try searching for promise and promises and you get totally different results just because of the plurality. This is just an example, but there are many more.
- npmjs.com search results are simply based on the relevance they have to the terms you put into the search box. npms.io combines the relevance with the modules score producing results with far greater quality.
3. AWESOME-NODEJS
Awesome-nodejs is another resource to search good npm packages.
4. Keep your code simple
- Keep your code simple and reusable
- Modularize your source code
- Use tool like Es-lint or JavaScript Beautifier in your editor
5. Write Asynchronous code
synchronous code is executed in sequence – each statement waits for the previous statement to finish before executing. Asynchronous code doesn't have to wait – your program can continue to run. You do this to keep your site or app responsive, reducing waiting time for the user.Synchronous vs Asynchronous
Every method in the fs module has synchronous as well as asynchronous forms. Asynchronous methods take the last parameter as the completion function callback and the first parameter of the callback function as error. It is better to use an asynchronous method instead of a synchronous method, as the former never blocks a program during its execution, whereas the second one does.
6. Put all your require statements at the top of the file
Because “require” statements are synchronous and will block the code execution, so it's better to block the program on-load instead of blocking randomly.7. Use Security Tools - NODESECURITY.IO and SNYK.IO
These allow us to check that our code is secure or not, and also check your open source module doesn't have vulnerabilities.8. Automatic restart your production servers on failure
To keep your production server up during failures use PM2, StrongLoop Process Manager, Forever and Forever-Monitor.
9. Automatically restart your development servers on source code changes
Just use nodemon instead of node to run your code, and now your process will automatically restart when your code changes.10. Use async/await and promises to avoid callback hell
JavaScript is a strange language. Once in a while, you have to deal with a callback that’s in another callback that’s in yet another callback.
Solutions to callback hell
There are four solutions to callback hell:- Write comments
- Split functions into smaller functions
- Using Promises
- Using Async/await
Comments
Post a Comment
In case of any query or suggestion please comment here.