Express JS â€ĸ Unit Learning Page

Environment & Routing

Learn how to set up an Express project, understand npm, create your first server, define routes, work with HTTP methods, and organize routes using routers.

Node.jsnpmExpress RoutingExpress Router
1

Express JS Environment

Before Express, make sure Node.js and npm are available.

🧠 ELI10 idea

Think of Node.js as the engine that runs JavaScript outside the browser. npm is the package manager that helps you bring useful JavaScript packages into your project. Express is a framework built on Node.js that makes creating web servers and routes easier.

🔎 Check your installation

Run these commands in your terminal:

node -v
npm -v

The source document shows Node v22.13.0 and npm 10.9.2 as example output.

Node.js
JavaScript runtime
→
npm
Package manager
→
Express
Web framework
2

Node Package Manager (npm)

npm helps you discover and install packages for Node.js projects.

npm is the package manager for Node. The npm Registry is a public collection of packages containing open-source code for Node.js, front-end web apps, mobile apps, robots, routers, and many other JavaScript needs.

Global

Development tools

Generally used for development tools and CLI-based packages.

npm install -g <package-name>
Local

Frameworks & libraries

Installed inside a project directory and used by that project.

npm install <package-name>
Remember

Why local?

A locally installed package belongs to the project directory where it is installed.

3

Create the Express Project

Build a small project named hello-world.

Create a folder and enter it

mkdir hello-world
cd hello-world

Create package.json

Run npm init. npm asks for project information and creates a package.json file.

npm init

Install Express

The document uses --save so Express is added as a project dependency.

npm install --save express

Install nodemon

nodemon restarts the server when files change, so you do not have to restart it manually after every modification.

npm install -g nodemon
💡 Dependency idea: Once dependencies are recorded in package.json, running npm install can install the dependencies listed there.
4

Your First Express Web Server

Create index.js and send a message from the server to the browser.

index.js

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

app.get('/', function(req, res){
  res.send("Hello world!");
});

app.listen(3000);
Browser→GET /→Express route→Hello world!

Start the application

E:\Dev\hello-world> nodemon index.js

Then open http://localhost:3000 in a browser. The source document shows a browser screenshot displaying Hello world! on page 5.

5

Understand the index.js Code

Read the program one line at a time.

1. Import Express

var express = require('express');

This imports the Express framework and makes it available through the express variable.

2. Create the app

var app = express();

This creates a new Express application instance. The app object can be used to configure routes, middleware, and the server.

3. Define a GET route

app.get('/', function(req, res){
  res.send("Hello world!");
});

req represents the incoming request and res represents the response sent back to the client.

4. Start listening

app.listen(3000);

The application listens for incoming HTTP requests on port 3000.

6

app.listen()

The method that starts the Express server listening for requests.

app.listen(port, [host], [backlog], [callback])
ArgumentMeaning
portThe port number on which the server accepts incoming requests. It is the only required parameter.
hostThe domain/host name. The source notes it may be set when deploying applications to the cloud.
backlogMaximum number of queued pending connections. The document gives a default of 511 and notes it is rarely changed unless high concurrent connection volume is expected.
callbackAn asynchronous function called after the server successfully starts listening; commonly used for a startup confirmation message.
7

Routing

Routing decides how your server responds to a particular HTTP request and URL path.

🧠 ELI10 analogy

Imagine a building with different doors. The URL path tells you which door you are asking for, the HTTP method tells you what kind of action you want, and the route handler decides what happens next.

The usual Express pattern

app.method(path, handler)
method
GET, POST, PUT, DELETE...
EXPRESS
ROUTE
path
URL such as /hello
handler
Function that receives req & res
response
What the client receives

Example: /hello

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

app.get('/hello', function(req, res){
  res.send("Hello World!");
});

app.listen(3000);

When you visit localhost:3000/hello, the server receives a GET request for /hello, executes its handler, and sends Hello World! back to the browser.

8

Different HTTP Methods at the Same Route

The same path can have different handlers for different HTTP methods.

app.get('/hello', function(req, res){
  res.send("Hello World!");
});

app.post('/hello', function(req, res){
  res.send("You just called the post method at '/hello'!\n");
});

app.listen(3000);
GET /hello→Hello World! POST /hello→POST response

The source demonstrates testing the POST request with Postman at http://localhost:3000/hello; page 9 includes a Postman screenshot showing the POST request and response.

app.all()

app.all('/test', function(req, res){
  res.send("HTTP method doesn't have any effect on this route!");
});

app.all() handles all HTTP methods at a particular route using the same function. The source notes that it is generally used for defining middleware.

9

Routers: Keep Routes Organized

Move related routes into their own file as the application grows.

🧠 Why use a router?

Putting every route in index.js can become difficult to maintain. A router lets you group related API routes in a separate file. For example, all routes related to a particular entity can live together.

things.js

var express = require('express');
var router = express.Router();

router.get('/', function(req, res){
  res.send('GET route on things.');
});

router.post('/', function(req, res){
  res.send('POST route on things.');
});

// export this router to use in index.js
module.exports = router;

index.js

var express = require('express');
var app = express();
var things = require('./things.js');

app.get('/hello', function(req, res){
  res.send("Hello World!");
});

app.use('/things', things);
app.listen(3000);

Both index.js and things.js should be in the same directory in the source example.

🔗 How the route is combined

app.use('/things', things)+router.get('/')→GET /things/

The / route inside things.js becomes a subroute of /things. Visiting localhost:3000/things/ therefore reaches the router's GET handler. The source's page 11 screenshot shows the resulting GET route on things. response.

📌 Key takeaway: Routers separate concerns, keep related code together, and help build maintainable applications. Define routes relating to an entity in a single file and include that router from index.js.
10

Interactive Routing Check

Use this mini activity to connect method + path + handler.

đŸŽ¯ Predict the response

Choose the request used in the routing examples.

Choose a method and path, then click Check route.
✓

Quick Revision Checklist

Before moving to the next Express topic, you should be able to explain these.

  • Why Node.js and npm are needed before starting Express.
  • The difference between global and local npm installation.
  • The purpose of package.json.
  • How to install Express and nodemon.
  • What express() does when creating the app.
  • What req and res represent.
  • How app.get() defines a GET route.
  • What app.listen(3000) does.
  • How different HTTP methods can use the same path.
  • Why express.Router() is useful for larger applications.
  • How app.use('/things', things) mounts a router.