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.
JavaScript runtime
Package manager
Web framework
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.
Development tools
Generally used for development tools and CLI-based packages.
npm install -g <package-name>Frameworks & libraries
Installed inside a project directory and used by that project.
npm install <package-name>Why local?
A locally installed package belongs to the project directory where it is installed.
Create the Express Project
Build a small project named hello-world.
Create a folder and enter it
mkdir hello-world
cd hello-worldCreate package.json
Run npm init. npm asks for project information and creates a package.json file.
npm initInstall Express
The document uses --save so Express is added as a project dependency.
npm install --save expressInstall nodemon
nodemon restarts the server when files change, so you do not have to restart it manually after every modification.
npm install -g nodemonpackage.json, running npm install can install the dependencies listed there.
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);
Start the application
Then open http://localhost:3000 in a browser. The source document shows a browser screenshot displaying Hello world! on page 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.
app.listen()
The method that starts the Express server listening for requests.
app.listen(port, [host], [backlog], [callback])
| Argument | Meaning |
|---|---|
| port | The port number on which the server accepts incoming requests. It is the only required parameter. |
| host | The domain/host name. The source notes it may be set when deploying applications to the cloud. |
| backlog | Maximum 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. |
| callback | An asynchronous function called after the server successfully starts listening; commonly used for a startup confirmation message. |
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)
GET, POST, PUT, DELETE...
ROUTE
URL such as /hello
Function that receives req & res
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.
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);
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.
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
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.
index.js.
Interactive Routing Check
Use this mini activity to connect method + path + handler.
đ¯ Predict the response
Choose the request used in the routing examples.
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
reqandresrepresent. - 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.