Although Node.js offers Web developers a number of benefits, when it comes time to handle requests for different URLs (“routes”), serve static files, or use templates to dynamically create the response, you either need to write the code yourself, or you can avoid a lot of coding by using a Web framework. Of all the Node frameworks in use today, Express is the most popular. It’s also the underlying library for a number of other frameworks.
If you haven’t gotten your hands dirty with Express yet, you’re in for a treat, because in today’s article, we’re going to learn how Express came about, what’s good about it, and finally, how to create a basic app in just minutes.
Some Background
Express was initially released in November 2010 by TJ Holowaychuk. In June 2014, rights to manage the project were acquired by StrongLoop, an American company that was itself acquired by IBM in September 2015. In January 2016, IBM announced that it would place Express under the stewardship of the Node.js Foundation incubator, which should help ensure its continued viability. Moreover, incubation status means that the project will receive mentoring and open source governance from the foundation. So far, so good! Express is going like gang busters and is currently on version 4.16.3.
Installing Express
It should go without saying that, being a Node.js framework, Express can be installed using NPM. You should install it globally so that it can be used to create a Web application using node terminal:
npm i -g express
Assuming you’ve already installed Node.js, create a directory to hold your application, and make that your working directory:
$ mkdir myapp $ cd myapp
Use the npm init command to create a package.json file for your application:
$ npm init
This command prompts you for a number of things, such as the name and version of your application. For now, you can simply press RETURN to accept the defaults for most of them, except for the application entry point:
entry point: (index.js)
Enter “app.js”, or whatever you want the name of the main file to be. If you don’t mind calling it “index.js”, press RETURN to accept the suggested default file name. Here’s a listing of the whole process:
PS I:My DocumentsExpressmy-express-app> npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults. See `npm help json` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg>` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. package name: (my-express-app) version: (1.0.0) description: entry point: (index.js) app.js test command: git repository: keywords: author: license: (ISC) About to write to I:My DocumentsExpressmy-express-app package.json: { "name": "my-express-app", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "test": "echo "Error: no test specified" && exit 1" }, "author": "", "license": "ISC" } Is this ok? (yes) PS I:My DocumentsExpressmy-express-app>
Now, install Express in the myapp directory and save it in the dependencies list. For example:
$ npm install -g express --save
Here’s the listing for that:
PS I:My DocumentsExpressmy-express-app> npm install express --save npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN my-express-app@1.0.0 No description npm WARN my-express-app@1.0.0 No repository field. + express@4.16.3 added 50 packages in 5.08s PS I:My DocumentsExpressmy-express-app>
The app.js Code
The app.js script starts a server and listens on port 8081 for connections. The app responds to GET requests to the root URL (/) with “Hello World”. For every other URL, it will automatically respond with a 404 Not Found.
var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World'); }); var server = app.listen(8081, function () { var host = server.address().address; var port = server.address().port; console.log("Example app listening at http://%s:%s", host, port); });
Save the app.js file and then run it with the following command:
$ node app.js
You should see the following output in the console:
Example app listening at http://:::8081
Open http://127.0.0.1:8081/ in a browser to see the following result:
Figure 1: “Hello World” being displayed
You can play with today’s code on runkit.com.
Conclusion
In today’s article, we learned how Express came about, what’s good about it, and how to create a basic app. In the next installment, we’ll look at how Express handles different request types and routes as well as how to use the Express generator, which creates the scaffolding for a full app with numerous JavaScript files, Jade templates, and sub-directories.