đ Introduction
Express.js is a fast, lightweight, and flexible web framework for Node.js. Before building web applications or REST APIs with Express, you need to install Node.js, initialize a project, and add the Express package using npm.
Information
đ Prerequisites
- Install Node.js (includes npm).
- Use a code editor such as Visual Studio Code.
- Basic familiarity with the command line.
đ Installation Steps
đ Step 1: Create a Project Folder
Create Project Directory
mkdir express-app
cd express-appThis creates a new folder named express-app and navigates into it.
đĻ Step 2: Initialize the Project
Initialize package.json
npm init -yThe command generates a package.json file that stores your project's metadata, scripts, and dependencies.
Tip
âŦī¸ Step 3: Install Express.js
Install Express
npm install expressThis command downloads Express.js and its required dependencies into the node_modules directory and records it under the dependencies section of package.json.
đ Project Structure After Installation
đ Step 4: Create Your First Express Application
index.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Welcome to Express.js!');
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});âļī¸ Step 5: Run the Application
Start the Server
node index.jsOpen your browser and visit http://localhost:3000. You should see the message "Welcome to Express.js!".
Success
đ Understanding package.json
| Field | Description |
|---|---|
| name | Name of the project. |
| version | Current version of the application. |
| main | Entry point of the application. |
| scripts | Commands used to run the project. |
| dependencies | Lists installed packages such as express. |
đ Verifying the Installation
Check Installed Packages
npm list expressThis command displays the installed version of Express.js in your project.
⥠Installing Express Globally (Optional)
In most cases, Express.js should be installed locally within each project. If you want to install the express-generator tool globally to quickly scaffold applications, use the following command:
Install Express Generator
npm install -g express-generatorImportant
đĄ Best Practices
- Initialize every project with package.json.
- Install Express locally using npm install express.
- Keep dependencies updated regularly.
- Use version control to manage your project files.
- Organize application code into folders such as routes, controllers, and middleware as the project grows.
đ Learn More
Visit the official resources for more information:
âĸ Node.js Official Website
âĸ Express.js Official Documentation