Intro to Express

Express is a light-weight, web application framework for writing RESTful APIs in Node.js.

We'll get to the RESTFUL API part soon, but for now, think of Express as a Node Package that gives us some extra tools for creating a web application using Node.

Let's create our first Node/Express app!

Our first Express App

1. Create a new directory for your project.

mkdir hello-express

2. Initialize Node

cd hello-express
npm init

3. Install Express

npm i express

Pause! Open your project file tree in your text editor and notice the new files that appear.

node_modules

Each time you use npm to install a new package, this folder will populate with the package you installed, along with all the dependencies of that package. That's why you'll see several files that look like they have nothing to do with the package you were trying to install. They are helper packages for the one you're going to use! More on this folder here.

package-lock.json

This file keeps track of your npm install history. As we just learned, when we use npm to install a package, there may be several other dependencies installing behind the scenes. Think of package-lock.json as the commit history for this activity. More on this file here.

No need to mess with any of these automatically generated files! NPM takes care of the modifications/additions for you each time you install/uninstall a package.

package.json:

Revisit the package.json file. Notice that express and the version number now shows up in the dependencies field. All third party modules will be listed here automatically when you use npm to install them (but the dependencies of those files will not be listed here).

4. Create your entry point file.

touch index.js

5. Import the Express module

index.js

const express = require('express');

6. Create an instance of an express app

index.js

const express = require('express');
const app = express();

7. Create a Home Route

index.js

const express = require('express');
const app = express();

app.get('/', function(req, res) {
    res.send('Hello, World!');
});

app.listen(8000);

We'll learn more about this code in the next lesson. For now, just copy it down.

8. Run nodemon

nodemon

Now visit localhost:8000 in your browser. Congratulations! You've just built your first express app!

Last updated