Do you know What are the common mistakes a node js developer makes? If not, let’s discuss 4 types of mistakes you should know about while working with node JS.

Before come to the topic let’s have an idea what is node.js and why we use node js.

Node JS is a cross-platform JavaScript run-time environment which allows you to execute JavaScript in server side. It’s completely open source framework so we can use this without any cost.

Why Node JS?

Node JS is based on asynchronous programming model and single threaded, non-blocking so all processes are concurrent and there is no parallelism. Code reusability is also achieved because of usage of same models and service interface between client-side and server-side.

Node JS provides super fast performance because of its unique features and MVC pattern but if we don’t follow quality standards performance and security issues can arrive.

So, I’ll be addressing 4 common Node JS problems or mistakes that I think are the most common mistakes done by Node JS developers.

1. Memory Leak

Memory leak is a very big issue as it causes serious performance issues in your application. So, while writing code you should keep in mind that the piece of code doesn’t cause any memory leaks.

Following are some of the ways to ignore this issue.

a. MongoConnect:

If you are using MongoConnect (Well known for session management and it stores each session in the database) in a Production environment it causes memory leak issue.

So the problem looks like:

var session = require('express-session'), MongoStore = require('connect-mongo')(session); app.use(session({ store: new MongoStore({ mongooseConnection: db.connection, collection: config.sessionCollection }) })); 1 2 3 4 5 6 7 8 var session = require ( 'express-session' ) , MongoStore = require ( 'connect-mongo' ) ( session ) ; app . use ( session ( { store : new MongoStore ( { mongooseConnection : db . connection , collection : config . sessionCollection } ) } ) ) ;

Assign it only for your development environment.

The solution for the problem:

if (process.env.NODE_ENV === 'development') { app.use(session({ store: new MongoStore({ mongooseConnection: db.connection, collection: config.sessionCollection }); })); } 1 2 3 4 5 6 7 8 if ( process . env . NODE_ENV === 'development' ) { app . use ( session ( { store : new MongoStore ( { mongooseConnection : db . connection , collection : config . sessionCollection } ) ; } ) ) ; }

b. Global variable:

When we write code sometimes it happens that we forgot to write ‘var’ before variable so now this variable will be a global variable and it can cause a memory leak.

function welcomeMessage() { message = ‘This message will be global variable’; } 1 2 3 function welcomeMessage ( ) { message = ‘ This message will be global variable ’ ; }

So, in above WelcomeMessage function, we forgot to write var before message variable so it can cause a memory leak.

To prevent such type of issues you should write the first line of each file with ‘use strict’;.

2. Duplicate code:

Code reusability is a major practice that should be followed. A good developer writes code once and reuses it all over the product. Just copying & pasting same code in every function or file is a very bad practice. I am going to share some tips by which you can ignore such things.

Let’s understand with Example:

When you write REST API. We know that we need domain(Model like User, Member) information in each function.

Like there are 2 functions:

exports.readUser = function(){ // Write code to find user info } exports.updateUser = function(){ // Write code to find user info before update user } 1 2 3 4 5 6 exports . readUser = function ( ) { // Write code to find user info } exports . updateUser = function ( ) { // Write code to find user info before update user }

So, there is only 2 functions but it is possible that there is n number of function where you need Domain information. In your example, we need user information. So the best solution for this type of case?

Write a middleware for all API where you need Domain info.

'use strict'; module.exports = function(app) { // User Routes app.route('/api/user/read/:userId).get(users.read); app.route('/api/user/update/:userId).get(users.update); // Finish by binding the user middleware app.param('userId', users.userByID); }; 1 2 3 4 5 6 7 8 9 'use strict' ; module . exports = function ( app ) { // User Routes app . route ( '/api/user/read/:userId).get(users.read); app.route(' / api / user / update / : userId ) . get ( users . update ) ; // Finish by binding the user middleware app . param ( 'userId' , users . userByID ) ; } ;

So, write your findOne code in middleware so you can use user info in every function where you need. You can assign that domain info into request parameter.

3. Unused code and variable:

Sometimes when we write code then and some requirement change occurs, we need to change our code logic but we forgot to remove that unwanted code.

Can you imagine how big the problem it can cause?

Let say if you are working on that project and you did this type of mistake. After some time, the new developer works on that project. Now, what happens? that developer will be so confused because you know that block of code we are not using.

So, guys write code that way every new developer can understand what you write. In any case, you want to keep that code you can just comment it out with a proper description so a new developer can understand easily.

Also, keep in mind that please remove an unused variable.

4. Ignoring already available libraries

Express middleware provides most common use library already. Like for file system ‘fs’, if you want to use for the file path, there is a library called ‘path’. But we ignore these common libraries and require another library for this type of purpose.

So, we are increasing our physical memory for that of the library which is already available.

Conclusion :

If you think that you are too doing these types of mistakes, it can cause poor product performance and security issues which will further decrease productivity.

So, try to ignore these types of common mistakes. Try to write clean and smart code so everyone can understand. If you want to become very good programmer keep in mind that you are writing code for others not for you. There is couple good blog written by our experts based on a good guideline for REST API and Angular.JS so you can read and try to implement these measures into your code.

Habilelabs provides web based and mobile based application development services with high quality.

Hope you find this post “Node JS Mistakes” helpful, so don’t Forget to share with friends on Facebook.

If you have any query then ask me in the comment box.