Introduction
Now that you know what Express is, it’s time to install and configure it to set up a simple Express application. Think of this as setting up your kitchen before cooking a gourmet meal—you need the right tools and ingredients in place to get started.
Installing Express
Before using Express to create a web application, you need to install it with npm (Node Package Manager). Follow these steps:
Step 1: Initialize npm
Open a terminal or command prompt window, navigate to your project folder, and run the following command to initialize npm:
npm init -y
This will create two files: package.json and package-lock.json. These files manage your project’s dependencies, which are packages your application needs to function correctly.
Step 2: Install Express
To install Express version 4.0, run this command:
npm install express@^4.0.0
Once installed, your package.json file will list Express as a dependency:
{
"name": "my-project-folder-name",
"version": "1.0.0",
"dependencies": {
"express": "^4.17.1"
}
}
The ^ symbol before the version number means npm will allow updates to any version greater than 4.17.1 but less than 5.0.0. This ensures compatibility with your project while keeping your dependencies up-to-date.
Managing the node_modules Folder
As you install more dependencies, the node_modules folder will grow significantly. This folder contains all the packages your application depends on, but it doesn’t need to be tracked in source control like Git.
Using .gitignore
Create a .gitignore file in your project root and add the following entry:
node_modules/
This ensures that the node_modules folder is ignored by Git. Alternatively, you can use GitHub's Node.js .gitignore template.
Creating an Express Application
An Express application is like a custom-built server tailored to handle specific requests and responses. It’s minimalistic, flexible, and relies on middleware packages to address various web development needs. This modularity allows you to structure your application in multiple "right ways."
How to Create an Application
Start by creating a file named app.js in your project folder. Open the file in your code editor and use the following code:
// Import the Express module
const express = require('express');
// Create the Express app
const app = express();
The app variable holds a reference to an Express application object. This object will act as the central hub for handling requests, configuring middleware, and routing responses in your application.
What You've Learned
In this lesson, you’ve learned how to:
- Install and manage Express using npm
- Understand the role of the
package.jsonfile in tracking dependencies - Use
.gitignoreto prevent thenode_modulesfolder from being tracked by Git - Create a basic Express application using the
express()function
With these foundational skills, you’re ready to build and expand your first Express application. This is just the beginning of your journey into building powerful web applications with Express!