forked from qaboxletstest/demo-api-testing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
126 lines (102 loc) · 3.45 KB
/
app.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const express = require('express')
const path = require('path')
const simpleAuth = require('./middleware/authentication')
const logger = require('./middleware/logger')
const fileUpload = require('express-fileupload')
const rateLimit = require("express-rate-limit");
const js2xmlparser = require("js2xmlparser");
const swaggerJsDoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
const customHeaders = require("./middleware/controlRespHeaders")
const checkContentType = require("./middleware/checkContentType")
const cors = require('cors')
const app = express()
const PORT = process.env.PORT || 5002
// Extended: https://swagger.io/specification/#infoObject
const swaggerOptions = {
swaggerDefinition: {
openapi: '3.0.0',
info: {
version: "1.0.0",
title: "Sample Rest APIs",
description: "QA BOX LET'S TEST",
contact: {
name: "QA BOX LET'S TEST"
},
servers: [`http://localhost:${PORT}`]
}
},
apis: ['./routes/api/*.js']
};
// Middleware
// Moved to a separate folder
// Init middleware
app.use(logger)
const corsOptions = {
origin: 'http://127.0.0.1:5500',
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'channelName'],
};
// Enable CORS
app.use(cors(corsOptions))
const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use("/api/docs", swaggerUi.serve, swaggerUi.setup(swaggerDocs));
// Rate-Limit
const createAccountLimiter = rateLimit({
windowMs: 20000, // 20 Seconds
max: 2, // start blocking after 2 requests
message: {
error: "Too many requests being raised from this IP, please try again after 20 Seconds"
}
});
// STEP 1
// app.get('/', (req, res) => {
// // res.send('<h1>Hello World</h1>')
// res.sendFile(path.join(__dirname, 'public', 'index.html'))
// })
//Set Static Folder
// app.use(express.static(path.join(__dirname, 'public')))
// Init Basic Authentication Middleware
app.use(simpleAuth)
// Check ContentType Middleware
app.use(checkContentType)
// XML/JSON Serialization Middleware
app.use(function (req, res, next) {
res.sendData = function (obj) {
if (req.accepts('json')) {
res.header('Content-Type', 'application/json');
res.send(obj);
} else if (req.accepts('application/xml')) {
res.header('Content-Type', 'application/xml');
var xml = js2xmlparser.parse("response", obj);
res.send(xml);
} else {
res.send(406);
}
}
next();
});
// Body Parser Middleware
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
// app.use(express.text())
app.use(fileUpload())
// Exclude Headers
app.use(customHeaders)
// API Members - persistence
app.use('/api/members', createAccountLimiter, require('./routes/api/members'))
// API Authors - persistence
app.use('/api/authors', createAccountLimiter, require('./routes/api/authors'))
// API File Upload
app.use('/api/upload', require('./routes/api/fileupload'))
// API Vehicles
app.use('/api/vehicles', require('./routes/api/vehicles'))
// API File Download
app.use('/api/download', require('./routes/api/fileDownload'))
// API Lag Example
app.use('/api/lag', require('./routes/api/lag'))
// Request Header Example
app.use('/api/sendheader', require('./routes/api/checkHeader'))
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`)
})