Do you know What are the common mistakes a node js developer make? If not, let's discuss 4 types
of mistakes you should know about while working with 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 common Node JS problems that I think are the most common mistakes done by
Node JS developers.
# 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. If you are using MongoConnect (Well known for session management and it stores session
for each user in database) in server side it can cause memory leak issue.
/* Problem Start */
var session = require('express-session'),
MongoStore = require('connect-mongo')(session);
app.use(session({
store: new MongoStore({
mongooseConnection: db.connection,
collection: config.sessionCollection
})
}));
/* Problem End */
Assign it only for your development environment:
/* Solution Start */
if (process.env.NODE_ENV === 'development') {
app.use(session({
store: new MongoStore({
mongooseConnection: db.connection,
collection: config.sessionCollection
});
}));
}
/* Solution End */
b. Global variable
When we write code sometime it happens that we forgot to write ‘var’ before
variable so now this variable will be global variable and it can cause memory leak.
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 memory leak.
To prevent such type of issues you should write the first line of each file with 'use strict';.
#Duplicate code
Code reusability is 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.
For 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
}
So there are only 2 function but it is possible that there are n number of function where
you need Domain information. In your example we need user information. So 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);
};
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.
#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,
new developer works on that project. Now what happen ? 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 proper description so
new developer can understand easily.
Also keep in mind that please remove unused variable.
# Ignoring already available libraries
Express middleware provides most common use library already. Like for filesystem ‘fs’, if
you want to use for file path, there is 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 library which are 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 are couple good blog written by our experts based
on good guideline for REST API and Angular JS so you can read and try to implement these
measures into your code.

You should Know, What are the Common mistakes a node js developer makes?

  • 1.
    Do you knowWhat are the common mistakes a node js developer make? If not, let's discuss 4 types of mistakes you should know about while working with 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 common Node JS problems that I think are the most common mistakes done by Node JS developers. # 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. If you are using MongoConnect (Well known for session management and it stores session for each user in database) in server side it can cause memory leak issue. /* Problem Start */ var session = require('express-session'), MongoStore = require('connect-mongo')(session); app.use(session({ store: new MongoStore({ mongooseConnection: db.connection, collection: config.sessionCollection }) })); /* Problem End */ Assign it only for your development environment: /* Solution Start */
  • 2.
    if (process.env.NODE_ENV ==='development') { app.use(session({ store: new MongoStore({ mongooseConnection: db.connection, collection: config.sessionCollection }); })); } /* Solution End */ b. Global variable When we write code sometime it happens that we forgot to write ‘var’ before variable so now this variable will be global variable and it can cause memory leak. 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 memory leak. To prevent such type of issues you should write the first line of each file with 'use strict';. #Duplicate code Code reusability is 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. For Example : When you write REST API. We know that we need domain(Model like User, Member) information in each function
  • 3.
    Like there are2 functions : exports.readUser = function(){ // Write code to find user info } exports.updateUser = function(){ // Write code to find user info before update user } So there are only 2 function but it is possible that there are n number of function where you need Domain information. In your example we need user information. So 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); }; 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. #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?
  • 4.
    Let say ifyou are working on that project and you did this type of mistake. After some time, new developer works on that project. Now what happen ? 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 proper description so new developer can understand easily. Also keep in mind that please remove unused variable. # Ignoring already available libraries Express middleware provides most common use library already. Like for filesystem ‘fs’, if you want to use for file path, there is 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 library which are 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 are couple good blog written by our experts based on good guideline for REST API and Angular JS so you can read and try to implement these measures into your code.