- Express.js Basics
- Express.js HOME
- Express.js Introduction
- Express.js Installation
- Express.js Basic App
- Express.js Routing
- Basics Routing
- Route Parameters
- Handling Query Strings
- Router Middleware
- Middleware
- What is Middleware?
- Application-Level Middleware
- Router-Level Middleware
- Built-In Middleware
- Error-Handling Middleware
- Third-Party Middleware
- Express.js HTTP
- Handling GET Requests
- Handling POST Requests
- Handling PUT Requests
- Handling DELETE Requests
- Templating Engines
- Using Templating Engines
- Setting Up EJS
- Setting Up Handlebars
- Setting Up Pug
- Request/Response
- Request Object
- Response Object
- Handling JSON Data
- Handling Form Data
- Static Files
- Serving Static Files
- Setting Up Static Folders
- Managing Assets
- Express.js Advanced
- Middleware Stack
- CORS in Express.js
- JWT Authentication
- Session Handling
- File Uploads
- Error Handling
- Databases
- Express.js with MongoDB
- MongoDB CRUD Operations
- Express.js with MySQL
- MySQL CRUD Operations
- Deployment
- Deploying Express.js Apps to Heroku
- Deploying Express.js Apps to AWS
- Deploying Express.js Apps to Vercel
Express.js Installation
Installing Express.js is the first step toward building fast, scalable backend applications. Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Because it is available as an npm (Node Package Manager) package, the setup process is integrated directly into the standard Node.js workflow.
node -v in your terminal. If it returns a version number, you are ready to go!
Installation Steps
1. Initialize a New Node.js Project
Before installing Express, you need a place for your project settings to live. Initializing a project creates a package.json file, which acts as a manifest for your application, listing your dependencies, scripts, and versioning.
npm init -y
The -y flag tells npm to skip the interactive questionnaire and generate a default file automatically. This is a huge time-saver when you just want to start coding immediately.
npm init. Without a package.json file, npm won't properly track your dependencies, making it difficult for other developers (or your production server) to run your code later.
2. Install Express
Once your project folder is initialized, you can fetch the Express package from the npm registry. Run the following command in your terminal:
npm install express --save
While modern versions of npm (version 5+) save dependencies by default, including the --save flag is a classic habit that ensures Express is listed under the dependencies section of your package.json. This allows anyone else who downloads your project to run npm install and automatically get Express.
.gitignore file in your root directory and add node_modules/ to it. You should never push the thousands of files in that folder to GitHub; instead, let others install them locally using your package.json.
3. Create Your First Express Application
Now that the library is installed, it’s time to write some code. Create a file named app.js in your project folder. This file will serve as the entry point for your web server.
const express = require('express');
const app = express();
const PORT = 3000;
// Define a basic route
app.get('/', (req, res) => {
res.send('Hello, Express! Your server is up and running.');
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
In this snippet, app.get defines a "route." It tells the server: "When a user visits the root URL (/) using a GET request, send back this text." The app.listen function tells the server to start "listening" for incoming traffic on a specific port.
3000 to another number like 5000 or 8080 to fix this.
4. Run the Application
To bring your server to life, use the node command followed by your filename in the terminal:
node app.js
You should see the message Server is running on http://localhost:3000. This confirms that your JavaScript code is now acting as a functional web server.
nodemon instead of node. Nodemon automatically restarts your server every time you save a file, saving you from manually stopping and starting the process. You can run it via npx nodemon app.js.
5. Access the Application
Open your preferred web browser and type http://localhost:3000 into the address bar. If everything was set up correctly, the browser will display the message: Hello, Express! Your server is up and running.
Summary
Setting up Express.js is a logical progression: you initialize your environment with npm init, pull in the framework via npm install, and write a script to handle requests. This lightweight setup is the "Hello World" of the backend world. From here, you can begin adding complex routes, connecting to databases, or serving HTML files to create a fully featured web application.