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! First, we must create a Node application, exactly as we have been doing. Then, we install Express using NPM.
mkdir hello-express
cd hello-express
npm init -y
touch index.js
npm i express
Pause! Open your project file tree in your text editor and notice the new files that appear.
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.
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).
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.
It's not necessary to upload the whole node_modules
folder to github, because anyone who forks/clones your code will be able to install all the necessary dependencies simply by running npm i
inside the cloned folder. This will download all the dependencies indicated in the package.json
file. To tell git to ignore the node modules folder, create a .gitignore
file in the root directory, and add node_modules
to it.
echo "node_modules" >> .gitignore
index.js
const express = require('express');
index.js
const express = require('express');
const app = express();
index.js
const express = require('express');
const app = express();
app.get('/', (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.
nodemon
Now visit localhost:8000 in your browser. Congratulations! You've just built your first express app!