?http://localhost:3000/v1/list
npm install cors
npm install @types/cors -D
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as cors from 'cors'
const whiteList = ['/list']
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(cors())
await app.listen(3000);
}
bootstrap();
中间件是在路由处理程序之前调用的函数,中间件函数可以访问请求和响应对象
中间件函数可以执行一下任务
import { VersioningType } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as session from 'express-session';
const whiteList = ['/list'];
function middleWareAll (req, res, next) {
console.log(req.originalUrl, '全局中间件');
if(whiteList.includes(req.originalUrl)){
next();
}else{
res.send('无权限!');
}
}
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableVersioning({
type: VersioningType.URI
})
app.use(session({secret: "WangShan", name: "xm.session", rolling: true, cookie: { maxAge: null}}));
app.use(middleWareAll);
await app.listen(3000);
}
bootstrap();