Core basics: Understand the basic Building blocks of Nest applications.
Build the project
# npm
npm i -g @nestjs/cli
# yarn
yarn global add @nestjs/cli
nest new project-nest
Copy the code
Running the above command creates the project-nest project directory, installs node_modules and some other boilerplate files, and creates a SRC directory.
Building a project using the Nest CLI creates an initial project structure. But it’s recommended: keep each module in its own dedicated directory.
# Initial project structureD.s │ ├─ App.Controll.js │ ├─ App.Controll.js App. The module. Which s │ ├ ─ ─ app. The module. The js │ ├ ─ ─ app. Module. Js. Map │ ├ ─ ─ app. Service. Which s │ ├ ─ ─ app. Service. Js │ ├ ─ ─ App. Service. Js. Map │ ├ ─ ─ the main, which s │ ├ ─ ─ the main, js │ ├ ─ ─ the main, js. Map │ └ ─ ─ tsconfig. Build. Tsbuildinfo ├ ─ ─ nest - cli. Json ├── Package.json ├─ ├.md ├─ SRC │ ├─ App.Controller. Spec App. The module. Ts │ ├ ─ ─ app. Service. The ts │ └ ─ ─ main. Ts ├ ─ ─test│ ├─ ├─ ├─ ├─ ├.class.txt, ├─ ├.class.txt, ├─ ├.class.txt, ├─ ├.class.txt, ├─ ├.class.txt, ├─ ├.class.txt, ├─ ├.class.txtCopy the code
The core file
├─ SRC │ ├─ App.Controller.spec.tsUnit test sample for base controller│ ├ ─ ─ app. Controller. TsExample of a basic controller with a single route│ ├ ─ ─ app. The module. Ts# The root module of the application│ ├ ─ ─ app. Service. TsBasic service with a single method│ └ ─ ─ the main ts# application entry file
Copy the code
Main. ts uses NestFactory to create the Nest application instance.
/ * * *@description: application entry file *@update: the 2021-09-07 21:12:01 *@author: Ada.H
*/
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
// Create a Nest application instance app using the NestFactory core class
The create() method returns an object that implements the INestApplication interface
const app = await NestFactory.create(AppModule);
// Start HTTP server
await app.listen(3000);
}
bootstrap();
Copy the code
Platform independent
Nest is intended to be a platform-agnostic framework, and any Node HTTP framework can be used after the adapter is created. There are two HTTP platforms that support out-of-the-box support: Express (the default) and Fastify.
The name of the platform | Platform features |
---|---|
platform-express |
As is known to all; Contracted; After actual combat test, suitable for production; Have a lot of community resources; It can be enabled without taking any action. |
platform-fastify |
High performance, low overhead, focus on efficiency and speed. |
// When the type is passed to the nestFactory.create () function, the app object will have functions specific to that particular platform.
const app = await NestFactory.create<NestExpressApplication>(AppModule);
Copy the code
Note: You do not need to specify a type unless you really want to access the underlying platform API.