In this article, we’re going to discuss another one of these frameworks, called Express.js. We’ll go over what exactly Express is, how it works, the benefits of using it, examples of several websites that use Express, how to install Express, some basic routing, how to serve static files, and, finally, how to use Middleware. At the conclusion of this article, you’ll hopefully have a better understanding of what Express is and how it can be useful to you.
What is Express.js?
How does Express work?
In general, express applications work by sending a sequence of calls to the middle tier. The middleware, as it’s called, has of course access to request objects and response objects. This means that using the Express framework gives you control over request and response objects. This gives you the power to add sessions, add post parameters, and template through ejs.
There are a lot of obvious benefits of this. It means that you can handle errors more efficiently and finish request and response cycles without interruption. It gives you the ability to make calls to the middleware at the top of the stack, as opposed to some other position. It also has features such as content negotiation, robust routing, environment based configuration, application level view options, session based flash negotiations, a dynamic view helpers. These are, of course, only some of the features available by using Express. In fact, Express is so popular that it’s just about the most popular Node.js framework out there, and it’s used by tons of companies, many of which you’ll be familiar with.
Express JS websites
Of the many websites that use Express, the most famous is probably MySpace. Although it’s kind of fallen out of favor since basically losing its publicity battle with the newer social network, Facebook, MySpace is still around and still boasts millions of users. In 2013, MySpace re-launched with a brand new site and all kind of new features – all powered by Express.
Another great site that uses Express is Yummly, which is a huge repository of recipes from all over the world. It includes adaptive recipes that have programmed amounts and recommendations for changing serving sizes.
Yet another website that uses Express is a cloud storage site called Koding. There are tons of cloud storage sites out there, but Koding takes the idea of cloud storage a step further by involving what they call a cloud ecosytem, which is a platform for all kinds of Express-powered features that work together for a coherent and powerful user experience.
The last website we’ll talk about that uses Express is called Storify. While other sites we’ve mentioned here have kind of served their own function, Storify is an aggregate of a lot of information regarding social media. It’s a platform that lets you access the top stories and information from your various networks, based off your interests and previous likes. It does all the work for you in order to gather information from sites such as YouTube, Facebook, Twitter, and many others.
Installing Express is very easy. The first step is to make sure that you’ve installed Node.js as it won’t work without it. Next, visit the Express site to see their instructions for installing Express. These are fairly easy, and mostly just involve a little housekeeping to make sure the kind of folders are there and accounted for that you’ll need. The instructions can be found here: http://expressjs.com/starter/installing.html.
Basic routing is an easy feature you’ll want to acquaint yourself with as you start to use Express. This is structured by assigning a particular endpoint, such as a URL or HTTP point. Then you assign a request method, such as GET, POST, or others. After that, just make sure that you keep each route as only handling one function to start.
Serve static files
Using middleware can be tricky, it’s a process that’s based around the idea that Express uses middleware to route commands between different levels in a program’s hierarchy. Middleware acts as a go-between for a bunch of different things, meaning that it can execute code, make changes to request and response objects, and call to other middleware in the pursuit of a command structure.