Express.js Tutorial for Beginners: Build Your First Web Server with Node.js

Learn Express.js step-by-step and build your first Node.js web server. Beginner-friendly tutorial covering installation, routing, middleware, APIs, and best practices.

What we do
  • WordPress Themes & Plugins
  • E-commerce, News Portals, Web Apps
  • Mobile & Desktop Applications
  • Professional IT Training Center
Node.js Development
Published: 2026-06-16 16:41:13

Article

Express.js is the most popular web framework for Node.js. It makes building web applications, REST APIs, dashboards, and backend services much easier than using Node.js alone. If you're learning backend development, Express.js is one of the first frameworks you should master.

What You'll Learn: How to install Express.js, create your first web server, define routes, handle requests, use middleware, and build a simple API.

Before starting, make sure Node.js and npm are installed on your computer. If not, read our guides: How to Install Node.js on Windows, Linux, and macOS and What Is NPM? Complete Guide for Beginners.

What Is Express.js?

Express.js is a lightweight and flexible web application framework for Node.js. It simplifies tasks such as routing, middleware management, request handling, response generation, and API development.

Why Developers Love Express.js
  • Simple and easy to learn
  • Fast and lightweight
  • Perfect for REST APIs
  • Large ecosystem and community
  • Works well with MongoDB, MySQL, PostgreSQL
  • Ideal for scalable applications

Create a New Project

Create a new directory:

mkdir express-tutorial
cd express-tutorial

Initialize npm:

npm init -y

This creates a package.json file for your project.

Install Express.js

Install Express.js using npm:

npm install express

After installation, Express.js will appear in your project's dependencies.

Express.js is now ready to use in your project.

Build Your First Web Server

Create a file named:

server.js

Add the following code:

const express = require('express');

const app = express();

const PORT = 3000;

app.get('/', (req, res) => {
    res.send('Hello World from Express.js!');
});

app.listen(PORT, () => {
    console.log(`Server running on port ${PORT}`);
});

Run the Server

node server.js

Open your browser and visit:

http://localhost:3000

You should see:

Hello World from Express.js!
Congratulations! You have built your first Express.js web server.

Understanding Routes

Routes determine how your application responds to client requests.

app.get('/about', (req, res) => {
    res.send('About Page');
});

app.get('/contact', (req, res) => {
    res.send('Contact Page');
});

Now these URLs will work:

  • /about
  • /contact

Handling Route Parameters

Dynamic routes allow you to capture URL values.

app.get('/user/:id', (req, res) => {
    res.send(`User ID: ${req.params.id}`);
});

Example:

/user/25

Output:

User ID: 25

Handling Query Parameters

app.get('/search', (req, res) => {
    res.send(`Search Term: ${req.query.q}`);
});

Example:

/search?q=nodejs

What Is Middleware?

Middleware functions execute before requests reach route handlers.

app.use((req, res, next) => {
    console.log('Request Received');
    next();
});

Middleware is commonly used for:

  • Authentication
  • Logging
  • Validation
  • Security
  • Error Handling

Handling JSON Requests

Enable JSON parsing:

app.use(express.json());

Create a POST endpoint:

app.post('/users', (req, res) => {
    res.json({
        message: 'User created',
        data: req.body
    });
});

Creating a Simple REST API

app.get('/api/products', (req, res) => {
    res.json([
        { id: 1, name: 'Laptop' },
        { id: 2, name: 'Mouse' }
    ]);
});

Visiting the endpoint returns JSON data.

Serving Static Files

Create a folder:

public

Add:

app.use(express.static('public'));

Express.js will automatically serve files from the public folder.

Error Handling

app.use((err, req, res, next) => {
    res.status(500).json({
        error: err.message
    });
});

Proper error handling improves application reliability and debugging.

If you encounter problems while developing, read: Common Node.js Errors and How to Fix Them

Recommended Project Structure

project/
│
├── server.js
├── package.json
├── routes/
├── controllers/
├── models/
├── middleware/
├── public/
└── views/

As your application grows, organizing files properly becomes increasingly important.

Best Practices

  • Keep routes modular
  • Use environment variables
  • Implement proper error handling
  • Validate user input
  • Use middleware effectively
  • Follow REST API conventions
  • Use version control (Git)

What to Learn Next

  1. Express.js Routing
  2. Middleware Deep Dive
  3. MongoDB with Mongoose
  4. MySQL with Node.js
  5. Authentication & JWT
  6. REST API Development
  7. Express.js Security Best Practices
  8. Deploying Express.js Applications

Conclusion

Express.js is one of the easiest and most powerful frameworks for building web applications with Node.js. By learning routing, middleware, request handling, and API development, you'll have the foundation needed to create professional backend applications.

Whether you're building a blog, e-commerce platform, HRMS, ERP system, REST API, or enterprise application, Express.js provides a solid foundation for scalable web development.

About ShasTech-IT

ShasTech-IT develops modern web applications, Node.js solutions, Android apps, ERP systems, HRMS platforms, WMS software, POS systems, and custom business solutions tailored to client requirements.

Contact WhatsApp