-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
73 lines (62 loc) · 2.02 KB
/
server.js
File metadata and controls
73 lines (62 loc) · 2.02 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
67
68
69
70
71
72
73
const express = require('express');
const mongoose = require('mongoose');
const db = require('./config/keys').mongoURI;
const data = require('./routes/api/data');
const availableExtendedRanges = require('./routes/api/available-extended-ranges');
const path = require('path');
const fs = require('fs');
const swaggerJsDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const app = express();
const port = process.env.PORT || 3334;
const swaggerOptions = {
swaggerDefinition: {
info: {
title: 'AirQt data API',
description: `Managing the data measured by the air quality sensors.
All endpoints are subject to global or endpoint-specific rate limits.
If limits are exceeded, a 429 Too Many Requests response will be returned.
Rate limit headers are present.
`,
contact: {
email: 'rradev@duck.com'
},
},
openapi: "3.0.0",
},
apis: ['routes/api/**/*.js']
}
const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.set('trust proxy', 1);
app.use(express.json());
mongoose
.connect(db, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log("MongoDB connected!"))
.catch(err => console.log(err));
if (process.env.NODE_ENV === 'production') {
app.use((req, res, next) => {
if (req.header('x-forwarded-proto') !== 'https') {
res.redirect(`https://${req.header('host')}${req.url}`)
} else {
next();
}
});
}
app.use('/api/data', data);
app.use('/api/available-extended-ranges', availableExtendedRanges);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const banner = fs.readFileSync(__dirname + "/banner.txt");
const server = app.listen(port, () => {
console.log(banner.toString());
console.log(`Server listening at http://localhost:${port}`);
});
module.exports = server;