Prerequisites
Please make sure that Node.js (>= 10.13.0) is installed on your operating system.
Setup
Setting up a new project is quite simple with the Nest CLI. With npm installed, you can create a new Nest project with the following commands in your OS terminal:
npm i -g @nestjs/cli nest new project-name
Result:
D:\TypeScript\Nestjs>npm i -g @nestjs/cli npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprec ated npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#dep recated npm WARN deprecated chokidar@2.1.8: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies. npm WARN deprecated fsevents@1.2.13: fsevents 1 will break on node v14+ and coul d be using insecure binaries. Upgrade to fsevents 2. C:\Program Files\nodejs\nest -> C:\Program Files\nodejs\node_modules\@nestjs\cli \bin\nest.js npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.1.2 (node_modules\@n estjs\cli\node_modules\chokidar\node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@ 2.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"} ) npm WARN notsup Unsupported engine for watchpack-chokidar2@2.0.0: wanted: {"node ":"<8.10.0"} (current: {"node":"14.7.0","npm":"6.14.7"}) npm WARN notsup Not compatible with your version of node/npm: watchpack-chokidar 2@2.0.0 npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules\@n estjs\cli\node_modules\watchpack-chokidar2\node_modules\chokidar\node_modules\fs events): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@ 1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64" }) + @nestjs/cli@7.5.1 added 513 packages from 353 contributors in 130.757s
D:\TypeScript\Nestjs>nest new project1 ? We will scaffold your app in a few seconds.. CREATE project1/.eslintrc.js (663 bytes) CREATE project1/.prettierrc (51 bytes) CREATE project1/nest-cli.json (64 bytes) CREATE project1/package.json (1890 bytes) CREATE project1/README.md (3370 bytes) CREATE project1/tsconfig.build.json (97 bytes) CREATE project1/tsconfig.json (339 bytes) CREATE project1/src/app.controller.spec.ts (617 bytes) CREATE project1/src/app.controller.ts (274 bytes) CREATE project1/src/app.module.ts (249 bytes) CREATE project1/src/app.service.ts (142 bytes) CREATE project1/src/main.ts (208 bytes) CREATE project1/test/app.e2e-spec.ts (630 bytes) CREATE project1/test/jest-e2e.json (183 bytes) ? Which package manager would you ?? to use? (Use arrow keys) > npm yarn √ Installation in progress... ? ? Successfully created project project1 ? Get started with the following commands: $ cd project1 $ npm run start Thanks for installing Nest ? Please consider donating to our open collective to help us maintain this package. ? Donate: https://opencollective.com/nest
The project directory will be created, node modules and a few other boilerplate files will be installed, and a src/ directory will be created and populated with several core files.
src |--app.controller.spec.ts |--app.controller.ts |--app.module.ts |--app.service.ts |--main.ts
Here’s a brief overview of those core files:
app.controller.ts |
Basic controller sample with a single route. |
app.module.ts |
The root module of the application. |
app.service.ts |
Basic service sample. |
main.ts |
The entry file of the application which uses the core function NestFactory to create a Nest application instance. |
The main.ts
includes an async function, which will bootstrap our application:
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(3000); } bootstrap();
To create a Nest application instance, we use the core NestFactory
class. NestFactory
exposes a few static methods that allow creating an application instance. The create()
method returns an application object, which fulfills the INestApplication
interface. This object provides a set of methods which are described in the coming chapters. In the main.ts
example above, we simply start up our HTTP listener, which lets the application await inbound HTTP requests.
Note that a project scaffolded with the Nest CLI creates an initial project structure that encourages developers to follow the convention of keeping each module in its own dedicated directory.
Platform
Nest aims to be a platform-agnostic framework. Platform independence makes it possible to create reusable logical parts that developers can take advantage of across several different types of applications. Technically, Nest is able to work with any Node HTTP framework once an adapter is created. There are two HTTP platforms supported out-of-the-box: express and fastify. You can choose the one that best suits your needs.
platform-express |
Express is a well-known minimalist web framework for node. It’s a battle tested, production-ready library with lots of resources implemented by the community. The @nestjs/platform-express package is used by default. Many users are well served with Express, and need take no action to enable it. |
platform-fastify |
Fastify is a high performance and low overhead framework highly focused on providing maximum efficiency and speed. Read how to use it here. |
Whichever platform is used, it exposes its own application interface. These are seen respectively as NestExpressApplication
and NestFastifyApplication
.
When you pass a type to the NestFactory.create()
method, as in the example below, the app
object will have methods available exclusively for that specific platform. Note, however, you don’t need to specify a type unless you actually want to access the underlying platform API.
import {NestExpressApplication} from '@nestjs/platform-express' //... const app = await NestFactory.create<NestExpressApplication>(AppModule);
Running the application
Once the installation process is complete, you can run the following command at your OS command prompt to start the application listening for inbound HTTP requests:
npm run start
Result:
D:\TypeScript\Nestjs\project1>npm run start > project1@0.0.1 start D:\TypeScript\Nestjs\project1 > nest start [Nest] 78260 - 09/25/2020, 12:05:56 PM [NestFactory] Starting Nest applicati on... [Nest] 78260 - 09/25/2020, 12:05:56 PM [InstanceLoader] AppModule dependenci es initialized +16ms [Nest] 78260 - 09/25/2020, 12:05:56 PM [RoutesResolver] AppController {}: +8 ms [Nest] 78260 - 09/25/2020, 12:05:56 PM [RouterExplorer] Mapped {, GET} route +5ms [Nest] 78260 - 09/25/2020, 12:05:56 PM [NestApplication] Nest application su ccessfully started +3ms
This command starts the app with the HTTP server listening on the port defined in the src/main.ts
file. Once the application is running, open your browser and navigate to http://localhost:3000/
. You should see the Hello World!
message.
Documentation @nestjs/core
Documentation @nestjs/common
Documentation @nestjs/websockets
Documentation @nestjs/microservices