This chapter builds the smallest possible web server with Express, then creates a NestJS project with the Nest CLI and runs its development server. It ends with a few notes on editor setup.
How Express and NestJS relate
Express is a minimal web framework for Node.js: it gives you routing and middleware and leaves the rest of the architecture to you. NestJS is a server framework that adds structure on top, with modules, dependency injection and decorators. Nest delegates HTTP handling to a platform adapter, and the default adapter is Express (@nestjs/platform-express). A Nest application is therefore an Express server under the hood, and you can use Express middleware directly. Swapping the adapter for Fastify is also supported.
Both need Node.js. Check what you have installed; for a fresh install, the LTS release from nodejs.org is the safe choice.
node --version
npm --version
A minimal Express server
Create a package.json in an empty folder and install Express.
mkdir hello-express
cd hello-express
npm init -y
npm install express
Add an index.js file. express() creates the application, app.get registers a handler for GET requests to a path, and app.listen opens a port.
// index.js
const express = require("express");
const app = express();
const port = process.env.PORT || 3000;
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.listen(port, () => {
console.log(`Listening on http://localhost:${port}`);
});
Run it with node index.js and open http://localhost:3000. During development, Node's built-in watch mode restarts the server whenever you save a file.
node --watch index.js
Creating a project with the Nest CLI
Nest projects are usually created with the official command-line tool, the Nest CLI. Install it globally and run nest new.
npm i -g @nestjs/cli
nest new hello-nest
cd hello-nest
npm run start:dev
nest new asks which package manager to use (npm, yarn or pnpm), scaffolds a project with TypeScript and testing already configured, and installs dependencies. If you prefer not to install globally, npx @nestjs/cli new hello-nest does the same. npm run start:dev runs nest start --watch, which recompiles and restarts on every change. Once it is up, http://localhost:3000 responds with "Hello World!".
The entry point is src/main.ts. NestFactory.create builds the application from the root module, AppModule, and starts listening.
// src/main.ts
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
The generated package.json includes the scripts you will use most.
Adding your first endpoint
The CLI's generate command (g for short) creates files and registers them in the nearest module.
nest generate controller hello
# short form
nest g co hello
Edit the generated src/hello/hello.controller.ts. When a handler returns an object, Nest serializes it as JSON.
import { Controller, Get, Query } from "@nestjs/common";
@Controller("hello")
export class HelloController {
@Get()
greet(@Query("name") name?: string) {
return { message: `Hello, ${name ?? "world"}!` };
}
}
curl "http://localhost:3000/hello?name=Nest"
# {"message":"Hello, Nest!"}
Editor setup
VS Code supports TypeScript out of the box, so completion and type checking work immediately. Nest projects ship with ESLint and Prettier configuration; install both extensions and enable format on save. To debug, start the server with npm run start:debug and attach the editor's Node.js debugger. WebStorm and other editors work the same way.
Summary
- Express is a lightweight framework built around routing and middleware; NestJS runs on Express by default and adds structure.
- An Express server takes
npm install express and a few lines of code. - A Nest project is created and started with
npm i -g @nestjs/cli, nest new and npm run start:dev. nest g generates building blocks such as controllers and wires them into a module for you.
0 comments
Sign in · Sign in to leave a comment.