-
Notifications
You must be signed in to change notification settings - Fork 229
Expand file tree
/
Copy pathapp.js
More file actions
66 lines (56 loc) · 1.93 KB
/
Copy pathapp.js
File metadata and controls
66 lines (56 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* istanbul ignore file */
/* eslint import/no-extraneous-dependencies: 0 */
// Dependencies
const express = require('express');
const bodyParser = require('body-parser');
const routes = require('./routes');
const routes2 = require('./routes2');
const swaggerJsdoc = require('../..');
const PORT = process.env.PORT || 3000;
// Initialize express
const app = express();
app.use(bodyParser.json()); // To support JSON-encoded bodies
app.use(
bodyParser.urlencoded({
// To support URL-encoded bodies
extended: true,
})
);
// Swagger definition
// You can set every attribute except paths and swagger
// https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md
const swaggerDefinition = {
info: {
// API informations (required)
title: 'Hello World', // Title (required)
version: '1.0.0', // Version (required)
description: 'A sample API', // Description (optional)
},
host: `localhost:${PORT}`, // Host (optional)
basePath: '/', // Base path (optional)
};
// Options for the swagger docs
const options = {
// Import swaggerDefinitions
swaggerDefinition,
// Path to the API docs
// Note that this path is relative to the current directory from which the Node.js is ran, not the application itself.
apis: ['./examples/app/routes*.js', './examples/app/parameters.yaml'],
};
// Serve swagger docs the way you like (Recommendation: swagger-tools)
app.get('/api-docs.json', async (req, res) => {
res.setHeader('Content-Type', 'application/json');
// Initialize swagger-jsdoc -> returns validated swagger spec in json format
const swaggerSpec = await swaggerJsdoc(options);
res.send(swaggerSpec);
});
// Set up the routes
routes.setup(app);
routes2.setup(app);
// Start the server
const server = app.listen(PORT, () => {
const host = server.address().address;
const { port } = server.address();
console.log('Example app listening at http://%s:%s', host, port);
});
module.exports = { app, server };