Difference between revisions of "NestJs"

(Created page with " =Setup= <syntaxhighlight lang="JavaScript"> $ npm i -g @nestjs/cli $ nest new project src ........app.controller.ts # The entry file of the application. It uses NestFact...")
 
Line 1: Line 1:
 +
 +
=Quick Commands for NestJS/CLI=
 +
<syntaxhighlight lang="JavaScript">
 +
 +
$ npm i -g @nestjs/cli          # installs nestjs
 +
$ npm install -g typescript      # if gives such error, Error: Cannot find module 'typescript'
 +
$ nest new project              # creates new nestjs project
 +
..
 +
 +
$ npm run start
 +
..
 +
$ nest g controller cats # creates controller
 +
 +
 +
</syntaxhighlight>
 +
 +
 +
https://docs.nestjs.com
 +
 +
  
 
=Setup=
 
=Setup=

Revision as of 09:27, 16 January 2019

Quick Commands for NestJS/CLI

$ npm i -g @nestjs/cli           # installs nestjs
$ npm install -g typescript      # if gives such error, Error: Cannot find module 'typescript'
$ nest new project               # creates new nestjs project
..

$ npm run start
..
$ nest g controller cats # creates controller


https://docs.nestjs.com


Setup

$ npm i -g @nestjs/cli
$ nest new project

src
   ........app.controller.ts # The entry file of the application. It uses NestFactory to create the Nest application instance.
   ........app.module.ts     # Defines AppModule, the root module of the application.
   ........main.ts           # Basic controller sample with a single route.

The main.ts includes an async function, which will bootstrap our application:

main.ts:

import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(ApplicationModule);
  await app.listen(3000);
}
bootstrap();

To create a Nest application instance, we are using the NestFactory. NestFactory is one of the most fundamental classes, it exposes a few static methods that allows creating application instance. The create() method returns an object, which fulfills the INestApplication interface, and provides a set of usable methods which are well described in the next chapters.

Running application

Once the installation process is complete, you can run the following command to start the HTTP server

$ npm run start

This command starts the HTTP server on the port defined inside the src/main.ts file. While the application is running, open your browser and navigate to http://localhost:3000/. You should see the Hello world! message.