Finally JavaScript has something useful for backend development...
let's quickly guide its basics....
npx @nestjs/cli new nestjs-example cd nestjs-example npm startsrc/main.ts
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(3000); } bootstrap();src/app.module.ts
import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; @Module({ imports: [], controllers: [AppController], providers: [AppService], }) export class AppModule {}src/app.controller.ts
import { Controller, Get } from '@nestjs/common'; import { AppService } from './app.service'; @Controller() // http://127.0.0.1:3000/** export class AppController { constructor(private readonly appService: AppService) {} @Get() // -> GET / getHello(): string { return this.appService.getHello(); } }this service will be injected in controller via constructor (see above)
src/app.service.ts
import { Injectable } from '@nestjs/common'; @Injectable() export class AppService { getHello(): string { return 'Hello World!'; } }as we can see, how we have only one endpoint: GET / which should hello:
http :3000response
HTTP/1.1 200 OK Connection: keep-alive Content-Length: 12 Content-Type: text/html; charset=utf-8 Date: Sun, 09 Feb 2020 13:13:44 GMT ETag: W/"c-Lve95gjOVATpfV8EL5X4nxwjKHE" X-Powered-By: Express Hello World!if you familiar with ng cli (@angular/cli from Angular framework) it would be very similar!
let's implement Maksimko's module:
npx @nestjs/cli generate module maksimko #CREATE /src/maksimko/maksimko.module.ts (85 bytes) #UPDATE /src/app.module.ts (324 bytes)verify main module has been updated:
src/app.module.ts
import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { MaksimkoModule } from './maksimko/maksimko.module'; @Module({ imports: [MaksimkoModule], controllers: [AppController], providers: [AppService], }) export class AppModule {}as you can see, now imports array is not empty and contains created MaksimkoModule:
src/maksimko/maksimko.module.ts
import { Module } from '@nestjs/common'; @Module({}) export class MaksimkoModule {}similarly, let's generate new controller
npx @nestjs/cli generate controller maksimko #CREATE /src/maksimko/maksimko.controller.spec.ts (507 bytes) #CREATE /src/maksimko/maksimko.controller.ts (105 bytes) #UPDATE /src/maksimko/maksimko.module.ts (182 bytes)now maksimko module should be updated:
src/maksimko/maksimko.module.ts
import { Module } from '@nestjs/common'; import { MaksimkoController } from './maksimko.controller'; @Module({ controllers: [MaksimkoController] }) export class MaksimkoModule {}and new controller created:
src/maksimko/maksimko.controller.ts
import { Controller } from '@nestjs/common'; @Controller('maksimko') // http://127.0.0.21:3000/maksimko/** export class MaksimkoController {}finally let's similarly create new service:
npx @nestjs/cli generate service maksimko #CREATE /src/maksimko/maksimko.service.spec.ts (474 bytes) #CREATE /src/maksimko/maksimko.service.ts (92 bytes) #UPDATE /src/maksimko/maksimko.module.ts (268 bytes)src/maksimko/maksimko.module.ts
import { Module } from '@nestjs/common'; import { MaksimkoController } from './maksimko.controller'; import { MaksimkoService } from './maksimko.service'; @Module({ controllers: [MaksimkoController], providers: [MaksimkoService] }) export class MaksimkoModule {}src/maksimko/maksimko.service.ts
import { Injectable } from '@nestjs/common'; @Injectable() export class MaksimkoService {}service
import { Injectable } from '@nestjs/common'; @Injectable() export class MaksimkoService { wtf(what: string): string { return `O.o ${what}?`; } }controller
import { Controller, Param, Post } from '@nestjs/common'; import { MaksimkoService } from './maksimko.service'; @Controller('maksimko') export class MaksimkoController { constructor(private maksimkoService: MaksimkoService) {} @Post('/wtf/:what') async wtf(@Param('what') what: string): Promise<string> { return this.maksimkoService.wtf(what); } }http post :3000/maksimko/wtf/ololooutput:
HTTP/1.1 201 Created Connection: keep-alive Content-Length: 10 Content-Type: text/html; charset=utf-8 Date: Sun, 09 Feb 2020 15:22:17 GMT ETag: W/"a-PbhLKehORdMno9Pz2s34zAbmS0I" X-Powered-By: Express O.o ololo?Nest is MIT licensed.