Veröffentlicht · wird verbessert
NestJS · Express-Anleitung · 1/6
Dieses Kapitel ist vorerst nur auf Englisch verfügbar.
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.
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 --versionCreate a package.json in an empty folder and install Express.
mkdir hello-express
cd hello-express
npm init -y
npm install expressAdd 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.jsNest 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:devnest 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, , and starts listening.
AppModule// 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.
| Script | What it does |
|---|---|
npm run start:dev | Development server in watch mode |
npm run start:debug | Watch mode with the debugger enabled |
npm run build | Compile TypeScript into dist |
npm run start:prod | Run the compiled dist/main.js |
npm run test / npm run test:e2e | Unit tests / end-to-end tests |
npm run lint | Run ESLint |
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 helloEdit 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!"}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.
npm install express and a few lines of code.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 Kommentare
Anmelden · Melde dich an, um einen Kommentar zu schreiben.
Schreib den ersten Kommentar.