Last active
July 7, 2019 08:30
-
-
Save mattmccabe/8e5586232053d0944a9d7efde738cb22 to your computer and use it in GitHub Desktop.
NestJS Swagger Setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function bootstrap() { | |
| const server = express(); | |
| const app = await NestFactory.create(ApplicationModule, server); | |
| const options = new DocumentBuilder() | |
| .setTitle('My API') | |
| .setDescription('Rest API') | |
| .setVersion('1.0') | |
| .setSchemes("https") | |
| .build(); | |
| const document = SwaggerModule.createDocument(app, options); | |
| //Add securityDefinitions to the api specification | |
| (<any>document)["securityDefinitions"] = { | |
| "x-api-key": { | |
| "type":"apiKey", | |
| "name": "x-api-key", | |
| "in":"header" | |
| }, | |
| 'jwt-auth' : { | |
| 'type' : 'apiKey', | |
| 'name': 'Authorization', | |
| 'in': 'header' | |
| } | |
| }; | |
| (<any>document)["security"] = { 'x-api-key' : [], 'jwt-auth':[]}; | |
| //Setup swagger at the swagger-ui proute | |
| SwaggerModule.setup('/swagger-ui', app, document); | |
| //Expose the swagger specification | |
| app.use('/swagger-spec.json', (req, res, next) => res.send(document)); | |
| await app.init(); | |
| try { | |
| const sslKey = readFileSync(process.env.EXPRESS_SSL_KEY_FILE, 'utf8'); | |
| const sslCert = readFileSync(process.env.EXPRESS_SSL_CERT_FILE, 'utf8'); | |
| const caCerts = process.env.EXPRESS_SSL_CA_CERTS.split(',').map((srcFile) => readFileSync(srcFile, 'utf8')); | |
| const sslCiphers = process.env.EXPRESS_SSL_CIPHERS; | |
| const serverOptions = { key: sslKey, cert: sslCert, ca:caCerts, ciphers:sslCiphers, requestCert:true, rejectUnauthorized: false}; | |
| https.createServer(serverOptions, server).listen(process.env.EXPRESS_SSL_PORT); | |
| console.log(`Server listening on ${process.env.EXPRESS_SSL_PORT}`); | |
| } catch (e) { | |
| console.log(e); | |
| } | |
| } | |
| bootstrap(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment