快速开始

快速开始

本部分主要介绍 Nest.js 项目的基本搭建与请求处理相关内容,建议是直接下载官方的 TypeScript 模板作为项目初始化模板:

$ git clone https://github.com/nestjs/typescript-starter.git project

$ cd project

$ npm install

$ npm run start
import { NestFactory } from "@nestjs/core";

import { ApplicationModule } from "./modules/ApplicationModule";

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

  await app.listen(3000);
}

bootstrap();

控制器

Nest Controller

// ApplicationModule.ts

import { Module } from "@nestjs/common";

import { HelloController } from "../controller/HelloController";

@Module({
  modules: [],

  controllers: [HelloController]
})
export class ApplicationModule {}
// HelloController.ts

import { Controller, Get } from "@nestjs/common";

@Controller("/")
export class HelloController {
  @Get()
  hello() {
    return "Next.js Boilerplate @ 王下邀月熊";
  }
}
import { Controller, Get, Post } from "@nestjs/common";

@Controller("cats")
export class CatsController {
  @Post()
  create() {
    // TODO: Add some logic here
  }

  @Get()
  findAll() {
    return [];
  }
}
下一页