#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Node.js & Express

Introduction to Node.js & Express

Node.js is a runtime that lets you run JavaScript outside the browser, most often on a server. Express is a small framework built on top of Node.js that makes writing web servers and APIs much simpler. Together they let a front-end developer use the same language on both sides of a web application: React or plain JavaScript in the browser, and Node.js with Express on the server that stores data and answers requests.

What Node.js adds to JavaScript

Node.js uses the same JavaScript engine as Chrome, V8, but replaces browser features with server features. There is no document or window. Instead you get modules for reading files (fs), working with paths (path), making network servers (http) and more. Node.js handles input and output without blocking: while it waits for a database or a file, it keeps serving other requests. That makes it a good fit for APIs that spend most of their time waiting on input and output.

Why Express on top of Node

You can build a server with Node's built-in http module, but you must parse URLs, match methods, read request bodies and set headers yourself. Express provides:

  • Routing: app.get('/users', ...) and app.post('/users', ...) map methods and paths to handler functions.
  • Middleware: a chain of functions that run on every request, for parsing JSON, logging, authentication and so on.
  • Helpers: res.json(), res.status(), req.params and req.query save a lot of repetitive code.

Your first Express server

mkdir my-api && cd my-api
npm init -y
npm install express
// index.js
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.use(express.json());   // read JSON request bodies

app.get('/', (req, res) => {
  res.send('Server is running');
});

app.get('/api/greet/:name', (req, res) => {
  const { name } = req.params;          // from the URL path
  const lang = req.query.lang || 'en';  // from ?lang=hi
  const message = lang === 'hi' ? `Namaste, ${name}` : `Hello, ${name}`;
  res.json({ message });
});

app.listen(PORT, () => {
  console.log(`Listening on http://localhost:${PORT}`);
});

Run it with node index.js and open http://localhost:3000/api/greet/Asha?lang=hi in a browser. You should see a JSON reply. Node 20 also supports node --watch index.js, which restarts the server whenever you save, so you do not need to stop and start it by hand.

How a request flows through Express

  1. A client sends an HTTP request, for example GET /api/greet/Asha.
  2. Express runs each matching middleware in the order you registered them.
  3. It finds the first route whose method and path match and calls its handler with req and res.
  4. The handler sends exactly one response, with a status code and a body.

If no route matches, Express replies with a default 404 page.

Early stumbling blocks

The first is mixing module systems. require is CommonJS. If your package.json contains "type": "module", you must use import express from 'express' instead, or Node throws "require is not defined". Pick one style per project.

The second is "address already in use" (EADDRINUSE). Another server, often an earlier copy you forgot to stop, is still holding the port. Stop it or choose another port. The third is a request that hangs forever: a handler that never calls res.send, res.json or next() leaves the client waiting until it times out.

Where this leads

Node.js runs JavaScript on the server, and Express adds routing, middleware and response helpers. With a server listening and a route returning JSON, you have the base for a full API. The next lesson explains RESTful design, the conventions most Express APIs follow for naming routes and choosing HTTP methods.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course