Skip to main content

Command Palette

Search for a command to run...

What is Express ? and how does it Work?

Published
2 min read
What is Express ? and how does it Work?

Express is a web framework for Node.js that helps developers build web applications and APIs easily. It provides a set of tools and features to handle things like routing, middleware, and managing HTTP requests and responses.

Why is Express Born ?

Imagine you want to build a simple website where users can visit a page and see a message. Without Express, you'd have to write a lot of code to handle web requests and responses in Node.js.

With Express, it becomes much simpler. Here's an example:

Without Express (basic Node.js server):

const http = require('http');

const server = http.createServer((req, res) => {
  if (req.url === '/') {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello, World!');
  } else {
    res.writeHead(404, {'Content-Type': 'text/plain'});
    res.end('Not Found');
  }
});

server.listen(3000, () => {
  console.log('Server running on port 3000');
});

With Express:

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

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

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

How to Install Express ?

1. Install Node.js

  • Go to nodejs.org and download Node.js if you don’t have it already.

2. Create a Project Folder

  • Open your terminal or command prompt, then create a folder for your project:

      mkdir myapp
      cd myapp
    

3. Install Express

  • Run this command to install Express;

  •   npm install express
    

That's it! Express is now installed and ready to use.