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:
- Is my app able to handle errors?
- Is my app able to survive the error and do not crash ?
- Is Session working correctly ?
- Is my app able to send emails?
- Is it doing some memory leaks ? (critical one)
- 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 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 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 !
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.
This post is part of Express Complete tutorial Series.
Part 1: Configuration of Express.
Part 2: Templating Using EJS.
Part 3: Handling routes.
Part 4: Handling Session.
Part 5: Finalising App.