Node.js Swagger模块中的前缀url
我使用nest js和swagger作为文档,但swagger模块忽略setGlobalPrefix()。 在我的环境中,api前缀是api_prefix=/api/v2,我在使用postman测试它时没有问题,因为端点/url确实有效,这是 但是招摇过市者无法获得/api/v2前缀,招摇过市者请求url是错误的 有什么想法吗?谢谢你的帮助 设置Node.js Swagger模块中的前缀url,node.js,typescript,swagger,nestjs,Node.js,Typescript,Swagger,Nestjs,我使用nest js和swagger作为文档,但swagger模块忽略setGlobalPrefix()。 在我的环境中,api前缀是api_prefix=/api/v2,我在使用postman测试它时没有问题,因为端点/url确实有效,这是 但是招摇过市者无法获得/api/v2前缀,招摇过市者请求url是错误的 有什么想法吗?谢谢你的帮助 设置 “`const-SWAGGER_PREFIX='/docs'; 异步函数bootstrap():Promise{ const app=wait Nes
“`const-SWAGGER_PREFIX='/docs';
异步函数bootstrap():Promise{
const app=wait NestFactory.create(AppModule);
如果(!process.env.SWAGGER_ENABLE | | process.env.SWAGGER_ENABLE=='1'){
//eslint在定义前禁用下一行@typescript eslint/no use
createSwagger(app);
}
use(bodyParser.json());
应用(头盔());
应用程序使用(
科尔斯({
来源:process.env.API|u CORS | |'*'
})
);
app.setGlobalPrefix(process.env.API_前缀| | API_默认_前缀);
const logInterceptor=app.select(CommonModule).get(logInterceptor);
app.useGlobalInterceptors(LogiInterceptor);
等待app.listen(process.env.API_PORT | | API_DEFAULT_PORT);
}
函数createSwagger(应用程序:INestApplication){
const version=require('../package.json')。version | |';
const options=new DocumentBuilder()
.setTitle(招摇过市标题)
.setDescription(招摇过市描述)
.setVersion(版本)
.setBasePath(process.env.API_前缀| | API_默认_前缀)
.addBearerAuth()
.build();
const document=SwaggerModule.createDocument(应用程序,选项);
SWAGGER模块。设置(SWAGGER_前缀、应用程序、文档);[![在此处输入图像描述][1][1]
//招摇过市-请求url为http://localhost:5000/user/profile
应该是什么http://localhost:5000/api/v2/user/profile
您确定这行
app.setGlobalPrefix(process.env.API_PREFIX || API_DEFAULT_PREFIX);
这一行的计算结果是一样的
.setBasePath(process.env.API_PREFIX || API_DEFAULT_PREFIX)
在设置api的全局前缀(在函数
引导
中调用setGlobalPrefix
)之后,应该创建swagger(在函数引导
中调用createSwagger
),然后它将自己找到api前缀,并在任何请求之前加上前缀
异步函数引导():Promise{
const app=wait NestFactory.create(ApplicationModule);
app.setGlobalPrefix(process.env.API_前缀| | API_默认_前缀);
如果(!process.env.SWAGGER_ENABLE | | process.env.SWAGGER_ENABLE=='true'){
createSwagger(app);
}
等待app.listen(process.env.API_PORT | | API_DEFAULT_PORT);
}
函数createSwagger(应用程序:INestApplication){
const version=require('../package.json')。version | |';
const options=new DocumentBuilder()
.setTitle(招摇过市标题)
.setDescription(招摇过市描述)
.setVersion(版本)
.build();
const document=SwaggerModule.createDocument(应用程序,选项);
SWAGGER模块设置(SWAGGER_前缀、应用程序、文档);
}
.setBasePath(process.env.API_PREFIX || API_DEFAULT_PREFIX)