Express is a fast, unopinionated, minimalist framework for Node.js.
You can use Express to write dynamic web applications on the backend that use the Node APIs and runtime environment as a high-performance web server.
Basically, if you know modern JavaScript then you know enough to run a web server and build your own web sites, web APIs, or web applications in a jiffy with Express.
First things first, you’ll need to download and install Node before continuing and you might need to put down your phone for a few minutes to follow through (sorry, but it’s probably good for you).
Setting up Express with npm
- Create the project directory.
- Create the
app.js
file in your project directory. - Open a Terminal or Command Line window on the project directory and run the following commands.
- Run
npm init
for configuring your project. - Follow the terminal instructions to create your
package.json
file and set your entry point asapp.js
when prompted. - Run
npm install express
to install the Express module (library).
Your project directory should look more or less like this:
..
├── /project
│ ├── app.js
│ ├── node_modules
│ ├── package.json
app.js
is a file that will contain your Express logic for setting up your project and running your HTTP web server. Your file could be named anything but most people follow an established convention when it comes to naming and name it app.js
.
node_modules
is a directory where the dependencies or the code required to use Express and other modules or libraries are stored.
The package.json
is a file that the npm tool uses to manage dependencies (among other things) for your project. It stores information about what type of modules your project uses so anyone running it can obtain them easily via npm.
It should look somewhat like this (don’t worry about the line that reads Error):
{ "name": "project", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
Running Your First Web Server
Open the app.js
file and write the following code:
const express = require('express'); const app = express(); app.get('/', (request, response) => response.send('Hello, world!')); app.listen(3000, () => console.log('Server started running!'));
Save your changes, run the node app.js
command on your Terminal, and open your web browser.
On your web browser, navigate to the http://localhost:3000 page. You should be able to see your ‘Hello, world!’ page from your app.js
file.
Pretty fast and easy, right? I told you!
Breaking it Down
However, to see what is really going on let’s break down our code line for line.
On lines 1 and 2, we are exporting the code from our Express module and we are obtaining our app
object by making a call to express()
. These objects are, again, named by convention.
app
is an object that provides convenience methods for handling our routes, HTTP requests and responses, starting up our server, and more.
On line 4 we are defining a request handler function for the route /
, which is the index of our page or the first page to load when visitors arrive at our web domain.
The get
method specifies that this code will only run when an HTTP GET
request is made to our route. Our request handler function has access to two objects, the request
and the response
. Express fills these two objects with a lot of useful information (try logging them to the console!).
On line 6 we are starting up the server on port 3000 of our computer (localhost) and running some code when it starts. In our case we just log a message to the console and call it a day.
Getting on the Express
While this is all pretty nice, no one (except maybe grandma) is going to be impressed by our simple ‘Hello, world!’ web server.
Express gives you a minimalist and elegant software interface for building more complex web projects. It is so minimal that the whole API documentation consists of just five objects, which is pretty insane for a web framework.
The main benefit of Express is that it allows you to get off the ground quickly and build your projects without relying on a whole lot of boilerplate code or built-in ‘magic’.
To learn more about the Express way of doing things and building something that can change the world (or not, that’s fine too), try going over the documentation on the Express web page or consider reading the excellent book, Express in Action.