After the outsourcing client inadvertently leaked the API swagger URL, a security measure using express-basic-auth was implemented to restrict access to the swagger docs. Subsequently, an attempt was made to integrate NestJS SWC into the system. However, during the integration process, a TypeError related to _expressbasicauth occurred as shown in the error details below.
Error details:/Users/gk/pray/dist/main.js:64
\], \_expressbasicauth({
^
TypeError: \_expressbasicauth is not a function
at bootstrap (/Users/gk/pray/dist/main.js:64:8)
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as serveStatic from 'serve-static';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import * as cookieParser from 'cookie-parser';
import { Logger } from '@nestjs/common';
import * as expressBasicAuth from 'express-basic-auth';
import { ConfigService } from '@nestjs/config';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);
app.enableCors({
origin: '*',
preflightContinue: false,
credentials: true,
});
app.use(
['/docs'],
expressBasicAuth({
challenge: true,
users: {
[configService.getOrThrow('SWAGGER_USER')]:
configService.getOrThrow('SWAGGER_PASSWORD'),
},
}),
);
const config = new DocumentBuilder()
.setTitle('Pray-App')
.setDescription('외주 API Docs')
.setVersion('3.0')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('/docs', app, document);
app.use('/image', serveStatic('image'));
//쿠키 설정
app.use(cookieParser());
const port = configService.getOrThrow('SERVER_PORT');
await app.listen(port);
Logger.log(`Port ${port}로 서버가 켜졌습니다.`);
}
bootstrap();