Giter Club home page Giter Club logo

daggerok / nestjs-example Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 1.0 1.45 MB

Finally JavaScript has something useful for backend development... All concepts will bd easily understood if you familiar with Angular 2+

License: MIT License

JavaScript 13.14% TypeScript 86.86%
nestjs nest github-actions github-action github-actions-javascript github-actions-python github-action-javascript github-action-node github-action-python github-actions-node

nestjs-example's Introduction

nestjs-example CI

Finally JavaScript has something useful for backend development...

let's quickly guide its basics....

create default boilerplate

npx @nestjs/cli new nestjs-example
cd nestjs-example
npm start

structure

starting point

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(3000);
}
bootstrap();

nest app module

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 {}

controller

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();
  }
}

service

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!';
  }
}

testing

as we can see, how we have only one endpoint: GET / which should hello:

http :3000

response

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!

implement a feature

if you familiar with ng cli (@angular/cli from Angular framework) it would be very similar!

new module

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 {}

new controller

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 {}

new service

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 {}

implementation

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);
  }
}

testing

http post :3000/maksimko/wtf/ololo

output:

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?

License

Nest is MIT licensed.

resoiurces

nestjs-example's People

Contributors

daggerok avatar dependabot[bot] avatar

Watchers

 avatar  avatar

Forkers

jmccar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.