-
Notifications
You must be signed in to change notification settings - Fork 8
/
app_test.js
84 lines (68 loc) · 2.19 KB
/
app_test.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const express = require('express')
const { Monitor } = require('../dist/')
const bodyParser = require('body-parser');
const path = require('path');
const createMiddleware = require('swagger-express-middleware');
const app = express()
Monitor.init(app, true)
app.get('/test', (req, res) => {
res.status(200)
res.send('test')
})
app.get('/testWithErrorMessage', (req, res) => {
res.status(400)
res.set("Error-Message", "Test Error Message")
res.send("test error message")
})
app.post('/test', (req, res) => {
res.status(200)
res.send('test post')
})
app.get('/testPathParameter/:parameter', (req, res) => {
res.status(200)
res.send('test post')
})
app.post('/testPathParameter/:param1/:param2', (req, res) => {
res.status(400)
res.set("Error-Message", `Test Error Message: param1=${req.params.param1} , param2=${req.params.param2}`)
res.send()
})
const router = express.Router()
router.get('/testRouter', (req, res) => {
res.send("test router")
})
router.get('/testPath/:parameter', (req, res) => {
res.send("parameter " + req.params.parameter);
})
router.post('/testPath/:parameter1/action/:parameter2', (req, res) => {
res.status(400)
res.set("Error-Message", `Test Error Message: param1=${req.params.parameter1} , param2=${req.params.parameter2}`)
res.send("test 2 path parameters")
})
app.use('/router', router)
const swaggerFile = path.join(__dirname, 'swagger/api/swagger-config.yml');
createMiddleware(swaggerFile, app, (error, middleware) => {
if (error) {
console.error('Error on Swagger Express', error);
}
app.use(
middleware.metadata(),
middleware.CORS(),
middleware.files(),
middleware.parseRequest(),
middleware.validateRequest()
);
app.use(bodyParser.text());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use((req, res, next) => {
const controllerName = req.swagger.path['x-swagger-router-controller'];
const operationId = req.swagger.operation['operationId'];
console.log(`Run ${controllerName}[${operationId}]`);
const controller = require(`./swagger/api/${controllerName}`);
controller[operationId](req, res);
next();
});
console.log('Swagger Express done');
});
module.exports = app