$ npm i -g @nestjs/cli
$ nest info
_ _ _ ___ _____ _____ _ _____
| \ | | | | |_ |/ ___|/ __ \| | |_ _|
| \| | ___ ___ | |_ | |\ `--. | / \/| | | |
| . ` | / _ \/ __|| __| | | `--. \| | | | | |
| |\ || __/\__ \| |_ /\__/ //\__/ /| \__/\| |_____| |_
\_| \_/ \___||___/ \__|\____/ \____/ \____/\_____/\___/
$ nest new project
生成的文件
src
├──app.controller.ts // 带有单个路由的基本控制器示例。
├── app.module.ts // 定义 AppModule 应用程序的根模块。
└── main.ts // 应用程序入口文件。它使用 NestFactory 用来创建 Nest 应用实例。
main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as bodyParser from 'body-parser';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useStaticAssets(`${__dirname}/assets`);
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
app.use(session({ secret: 'nest is awesome' }));
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Accept');
next();
});
await app.listen(3000);
}
bootstrap();
${__dirname}/assets
);添加静态目录
设置body的最大size
保存sesstion
需要额外安装npm install express-session @typescript/express-sesstion
设置项目需要的通用header
import { Controller,Get,Req, Put,Post,Delete,Body,Query,Headers,Header,ParamData,Param,Res,Session} from '@nestjs/common';
import { } from '@nestjs/core';
import { request, response} from 'express'
import { RequestHandler } from '@nestjs/common/interfaces/http/http-server.interface'
import { AppService } from './app.service';
import { SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG } from 'constants';
import { IncomingMessage } from 'http';
import { Response, Request } from 'express';
import { } from 'express-session';
@Controller('/api/test')
export class AppController {
constructor(private readonly appService: AppService) {}
@Get('getapi')
getapi(@Query() query: any) {
return {name: 'api return json', queryData: query};
}
@Put('put-api')
putApi(@Query() query: any,) {
return {queryData: query};
}
@Post('post-api')
postApi(@Query() query, @Body() body: any) {
return query;
}
@Delete('delete-api')
deleteApi(@Query() query) {
return query;
}
@Get('heders')
otherParam (@Headers() headers: any) {
return headers
}
@Get('header')
@Header('dd','cc')
Header() {
return {};
}
@Get()
some(@Req() req: Request, @Res() res: Response, @Session() Sess: Express.Session) {
// console.log(req);
if(req.session.name == undefined) {
req.session.name = 'dddsss';
}else{
req.session.name = 'agdadsf';
console.log(req.session.name)
}
res.set('myheader', 'zengwe');
res.send({}); // 必须res.send();
}
@Get('/:userId')
pathparam(@Param('userId') userId: number) {
return {userId: userId}
}
}
要定义@Req,@Res 需要安装npm install --save-dev @types/express
定义这个controller的路由开头,可以不设置
如: 127.0.0.1:3000/api/test/getapi?param=123 => AppController.getapi
127.0.0.1:3000/api/test => AppController.some
像静态目录、session、cookie都是用express方法在main.ts设置就可以, @Module中的许多方式都是和Angular极其相似,明天总结下文件上传