Express.Js Middleware, What is next()?

You may have noticed use of next() function while looking at node.js code.

Express is a routing web framework which utilize essentially a series of middleware calls. piece of code for a simple route could be like bellow.

app.get('/users/:id', function(req, res) {
 // logic to fetch user for provided id.
});

and with a additional parameter next

app.get('/users/:id', function(req, res) {
 var user_id = req.params.id;
 if(user_id) {
 // do something
 } else {
 next(); // here comes middleware.
 }
});

So what is next() doing here. In the example, for instance, you might look up the user in the database against provided user_id but if user not found in database, It passes control to the next matching route. It is a more flexible and powerful tool that could sit between/before the routes and can perform specific tasks for you.

Let say you want to perform check if user is authenticated  and have a started session with app, It is much easy to deal with that using middleware instead of specifically check at each route.

app.user(function(req, res, next) {

if (req.session.auth) {
next();
} else {
res.redirect("/auth");
}
});

It will check session before each route as long as we put this code before our routes. Express will run middleware in the order added to the stack. The router is one of these middleware functions. As long as you get your above function into the stack before the router, it will be used by all routes and things will work.

There is list of common actions you can perform using middleware. such as logger, bodyParser, cookieParser, errorHandler etc.

Web development is war of tools these days!

Today development is war of tools. more better tools lead to faster turn around. a better tool could help you to achieve the same task in few minutes which you are expected to finish in hours.

I can give you idea of many tools in this article which really makes life easy. during development you will find a wide range of tools which are actually more powerful than we think at first. exploring the features of that tool is the key, then keep yourself upgrade with new features and upgrades

If i am talking about tools for development, i will include everything which is meaningful while i am sitting at my desk. that could be anything – form a browser extension to IDE’s(code editors).

An IDE which consists of a source code editor, automation tools, Snippets support, debugger and a better auto-complete system for supported languages could lead to fast development compare to a one which lacks these features. I strongly believe opensource IDE’s are more powerful these days. Best example is Brackets is more efficient for any kind of development than Dreamweaver, both are made by same vendor. but opensource community could boost product development. I have found a amount of extensions for brackets (which can be installed using extension manager) can do many tasks more easily without leaving even leaving interface. i could not these things expect from Dreamweaver in next couple of releases.

Another example is Sublime text which another hero in world of IDE’s. i find it more faster and lightweight than any other code editor.

There are other tools like Navicat which is well-designed Graphical User Interface for database management and development software, it includes support for MySQL, MariaDB, SQL Server, Oracle, SQLite and/or PostgreSQL. i handles connections and session very smoothly, you don’t need to always open phpmyadmin always. there are others sqlyog etc.

Here i can talk about couple of Firefox/chrome extensions which provide enhanced features and are helpful during development. you need to explore development tags for these browser while searching.

Here i didn’t mentioned any tools/frameworks which we used during development like bootstrap, JavaScript libraries etc and a long list.

Soccer Blitzkrieg – Germany Vs Brazil

Brazil goes down, Germany was overwhelming and spectacular.

In tweets and popular media, terms used “Soccer Blitzkrieg”. term known as “blitzkrieg” is a highly mobile form of infantry and armour working in combined arms teams. It is more meaningful to Germans,  forces a breakthrough into the enemy’s line of defense through a series of short, fast, powerful attacks. yes this was German tactical and operational methodology during world war II.

In Tuesday GERMANY vs BRAZIL match, same kind of technique was used by Germany team.

That’s why you could see lot of tweets and posts used term blitzkrieg.

Sharukhan tweeted, “GER showed meaning of Blitzkrieg! A series of fast powerful attacks; 1ce in enemy territory, proceed 2 dislocate them using speed & s’prise”. Wall street journal used this term too. 

I never heard of this term, if i did then I must never tried getting familiar of what is it. though I read lot about World War I,II. Once it was my favorite topic of interest. I just researched bit more about this technique and wiki articles let me reminds of days in past.