Express provides everything you need to create powerful web applications utilizing Node.js. Another bonus is it has excellent community support.
Building applications often requires the same tasks to be repeated each time a new application is created. The web community recognized this fact with tools like Google Yeoman for automating the creation of web applications using common frameworks like Bootstrap. Express provides similar features for Node.js developers building web applications. (I have seen it called Express.js, ExpressJS, and just plain Express; I'll use the latter in this article.)
Piecing together this type of application can be confusing and eventually repetitive after multiple projects. My experience with Node-based web applications is limited, but the initial setup involves the following common tasks:
- Routing HTTP requests: How to parse and then act on requests, which means deciding which route to take based upon URL paths and how the request was made.
- Creating the Response: You've received the request, so how do to process it so how and what do you put in the Response object?
- Maintaining user sessions: Dealing with user session is a common chore with web applications. A common approach for Node-based applications seems to be Redis, but there are plenty of other options available.
- Cookie management: Offers functions and properties and middleware for dealing with cookies.
Express does the heavy lifting of setting up such applications with a few commands entered via its command line interface. Express builds applications utilizing the Node.js http module along with the Connect middleware framework. Basically, the middleware works with filters and providers, where filters handle requests, and providers are used to send responses back to the requestor.
Node.js is a basic requirement for using Express. With Node.js installed, you can use node package management system (npm) to download and install Express. This npm command handles will install it on your system: npm install –g express. The –g flag means Express is installed globally on your system.
Some legwork is required to go from installation to a web application. The Express website has some great resources for getting up to speed:
- The online documentation is excellent, particularly the API Reference and Guide links/sections.
- The applications area of the product site provides examples that you'll find helpful.
Once Express is installed, you can create a new application using the express keyword followed by application name: express application_name.
Figure A shows the results of this command — I created a directory and executed a command in that directory. Express creates a full directory structure for the application. You'll notice the following directories in Figure A.
- images: Images used by the application.
- stylesheets: A directory for all of the stylesheets used.
- views: Any views utilized in the application.
Creating a basic web application skeleton with Express command.
The bottom of the output in Figure A tells you to install dependencies utilized by the application; these dependencies are packages used by the application. By default, it uses quite a few, and you can add more if necessary.
Type the command in Figure A (npm install from application directory) and sit back and watch the output scroll by. Figure B shows the tail end of the output where it tells you the jade template engine and express were installed in the sub-directories specified.
Installing dependences for the Express-generated web application.
While it uses jade, there are other template engines available (like ejs) that you may utilize via command line switches. Figure C shows the syntax help for Express, which is the necessary switch —ejs.
Express command line options help is readily available.
Once the application is generated and dependencies installed, you can start the application within its directory via this command: node app.
Figure D shows the basic application loaded in Internet Explorer.
A basic Express application loaded in a browser.
Note: This is not the only way to create a web application using Express; the online documentation has information about the alternatives.
My experience with Node and Express is limited, but I can say Express greatly streamlines the process of creating Node web applications. (Some users compare Express to Rails for Ruby developers, but I'm not a Rails developers, so I'm not sure if the comparison is valid.) Express provides everything needed to build powerful applications utilizing Node, along with excellent community support. Many framework offerings have lost momentum after their release, but that's not the case with Express, which continues to be tweaked and enhanced.
Have you used Express? If so, please share your thoughts about it in the discussion.