Express.js Introduction and Setup

beginner
8 min

Express.js Introduction and Setup

Express.js is the most popular web framework for Node.js, powering everything from tiny hobby APIs to production systems at major companies. It is deliberately minimal: it gives you routing, middleware, and helpers for requests and responses, then stays out of your way. In this first lesson you will learn what Express is, why developers choose it, and how to set up a clean project ready for the rest of this series.

What Is Express.js?

Node.js ships with a built-in http module, but using it directly means manually parsing URLs, checking request methods with if statements, and hand-writing response headers. Express wraps that module with a small, expressive API. Instead of one giant request handler, you declare routes such as app.get('/users') and Express matches incoming requests to them automatically.

Express is often described as unopinionated. It does not force a folder structure, a database, or a template engine on you. That flexibility is why it sits at the heart of the classic MEAN and MERN stacks and why frameworks like NestJS are built on top of it.

Why Learn Express?

  • It has a huge ecosystem of middleware for logging, security, sessions, and more.
  • Its concepts (routing, middleware, request/response) transfer directly to Fastify, Koa, and even backend frameworks in other languages.
  • Job postings for Node.js developers almost always mention Express.
  • You can build a working API in under ten lines of code, which makes learning fast and rewarding.

Prerequisites

You need Node.js version 18 or newer and npm, which is bundled with Node. Verify both from your terminal:

bash
node --version npm --version

If either command fails, download the LTS installer from nodejs.org. Basic JavaScript knowledge (functions, objects, callbacks, and arrow functions) is assumed throughout this series.

Creating the Project

Create a folder and initialize a package.json, which records your dependencies and scripts:

bash
mkdir express-tutorial cd express-tutorial npm init -y npm install express

The npm install command downloads Express into node_modules and adds it to the dependencies section of package.json. As of Express 5, the framework requires Node 18+, and features like async error forwarding work out of the box.

A Quick Smoke Test

Create index.js with the smallest possible app to prove the setup works:

js
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Express is installed correctly!'); }); app.listen(3000, () => console.log('Listening on http://localhost:3000'));

Run node index.js, open http://localhost:3000 in a browser, and you should see the confirmation message. We will dissect every line of this file in the next lesson.

Helpful Scripts and Nodemon

Restarting the server after every edit gets old quickly. Install nodemon as a development dependency and add npm scripts:

bash
npm install --save-dev nodemon

Then in package.json add:

json
"scripts": { "start": "node index.js", "dev": "nodemon index.js" }

Now npm run dev restarts the server automatically whenever you save a file, which keeps your feedback loop tight while learning.

Key Takeaways

  • Express is a minimal, unopinionated web framework layered over the Node.js http module.
  • npm init -y plus npm install express is all it takes to start a project.
  • package.json tracks dependencies and defines convenient run scripts.
  • Use nodemon during development so the server restarts on every save.
  • Express 5 targets Node 18+ and improves async error handling.

Next up: Your First Express Server, where we walk through the request/response cycle line by line and send text, JSON, and status codes.