Getting Started with Express.js: Create a Server in Minutes

Ritik SinghRitik Singh
2 min read

If you’ve ever built a server with Node.js directly, you know it’s simple—but it can get messy fast as your project grows.
This is where Express.js shines! 🌟

Express is a lightweight, fast, and flexible Node.js framework that simplifies server-side coding, handling routes, requests, and responses easily.

In this blog, we’ll set up a basic Express server step-by-step.


🧠 Prerequisites

  • Node.js installed

  • Basic knowledge of JavaScript

  • A code editor like VS Code


📁 Step 1: Initialize Your Project

First, create a new folder and initialize it with npm:

mkdir express-server
cd express-server
npm init -y

This creates a package.json file to manage your project dependencies.


📦 Step 2: Install Express

Run the following command to install Express:

npm install express

📝 Step 3: Create Your Server File

Create a server.js file:

touch server.js

Open it and write the following code:

const express = require('express');
const app = express();

// Define a port
const PORT = 3000;

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

// Start the server
app.listen(PORT, () => {
  console.log(`Server is running at http://localhost:${PORT}`);
});

🔍 Quick Explanation:

  • express() initializes the app.

  • app.get('/', callback) sets up a route that listens for GET requests at the root URL /.

  • res.send() sends a response back to the client.

  • app.listen(PORT) starts the server.


▶️ Step 4: Run Your Express Server

In the terminal:

node server.js

You’ll see:

Server is running at http://localhost:3000

Visit http://localhost:3000 and you should see:

Hello from Express!

🚀 Boom! You’ve created an Express server!


🛠 Bonus: Add Another Route

Let's create another simple route:

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

Now, visiting http://localhost:3000/about will show "This is the About Page"!


🎯 Why Use Express?

  • Easy routing

  • Middleware support

  • Great community and libraries

  • Simplifies backend development

  • Perfect for APIs and web apps


📦 Conclusion

Express.js makes backend development faster, cleaner, and more scalable.
Today, you learned how to:

  • Set up a basic Express server

  • Handle routes

  • Serve responses

This is the first step towards building RESTful APIs, dynamic websites, and even full-stack applications!

0
Subscribe to my newsletter

Read articles from Ritik Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ritik Singh
Ritik Singh