Express Complete tutorial : Part 5

This is the last post on Express Complete tutorial series. Till now i have published how to configure, template, route and handle session.

In this final tutorial i am going to explain points to consider to finish up the Express app and get it ready for deployment.

Before deploying Express app, you should consider some important points. You must be very sure about the working of before hitting it to public. You should sure about these points:

  1. Is my app able to handle errors?
  2. Is my app able to survive the error and do not crash ?
  3. Is Session working correctly ?
  4. Is my app able to send emails?
  5. Is it doing some memory leaks ? (critical one)
  6. Is it able to resolve path of static files ?

Handling and Surviving Error:

Being a good developer you should always expect an error in your app. Error can happen any time under any situation.

Code which do not have error is not great code, the one survive it, is the one.

Fortunately, express have error handling mechanism. In every function call, you have one error callback variable. by using that you can code error prevention stuff.
For e.g

var express=require('express');
var app=express();

app.get('/',function(req,res){
res.send('hello world');
});

app.listen(3000);

This code is prone to error, what if user try to access ‘/home’. In that case express will throw 404 error but your code is not able to handle it. It should be like.

var express=require('express');
var app=express();

app.get('/',function(err,req,res){
if(err){
res.send('Some issue is here');
//take action accordingly
}
else{
res.send('hello world');
}
});

app.listen(3000);

This one is good practice.

Checking Sessions?

Like i have explained in last tutorial Sessions are very important. In every router code, you must validate whether the session is been set and if it is then what is the session variable and take action accordingly.

Sending emails in Express app?

E-mails are proven way to gain the user base and trust. Your app should also able to send e-mails in particular occasion to increase the level of personalisation with user.

I have explained about sending emails using Express in this tutorial.

Is there any memory leak?

Memory leak happens when program capture the amount of memory for execution and do not release them after finishing the execution.

It is very important to consider because unlike the development environment, production is more vulnerable to attacks !

leak-gc-events

To figure out this there is one popular and awesome package called memwatch. By using memwatch you can monitor and analyse the memory leaks. Of course you have to do it manually !

Reference reading : Mozilla hacks blog on memwatch

Resolving path in Express app?

Always using a relative path is good practice in development. However, if you don’t want that burden you can use path. For more information on using it, visit the official doc.

Shahid (UnixRoot) Shaikh

Hey there, This is Shahid, an Engineer and Blogger from Bombay. I am also an Author and i wrote a programming book on Sails.js, MVC framework for Node.js.

Related Posts

5 Comments

  1. Thanks a lot Shahid! I have been searching for an end to end simple tutorial for express.js. This was very simple yet striking on the key points…very helpful!!

  2. I must thank you for your tutorials, been learning node,js for the last week and I love it, now if you did one on using haversine to search in mySQL with node,js, that would be great. LOL.

  3. Good day, Shahid .
    English isn’t my native language.

    I’ve read this 5 articles about NodeJS.
    Thanks for your courses. They are very simple and capacious simultaneously.
    You have a talent to teach another people.

    Best regards.

Leave a Reply

Your email address will not be published. Required fields are marked *