-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
91 lines (75 loc) · 2.09 KB
/
app.js
File metadata and controls
91 lines (75 loc) · 2.09 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import cors from 'cors';
import https from 'https';
import yaml from 'yamljs';
import xss from 'xss-clean';
import dotenv from 'dotenv';
import helmet from 'helmet';
import express from 'express';
import 'express-async-errors';
import mongoose from 'mongoose';
import auth from 'express-basic-auth';
import swagger from 'swagger-ui-express';
import rateLimiter from 'express-rate-limit';
import errorHandler from './middleware/error-handler.js';
import parksRouter from './routes/parks.js';
import { reschedule } from './utils/index.js';
import path from 'path';
import { fileURLToPath } from 'url';
const swaggerDoc = yaml.load('./swagger.yaml');
dotenv.config();
const app = express();
// app.use(xss());
// app.use(cors());
app.use(helmet({
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false,
// crossOriginOpenerPolicy: false,
// crossOriginResourcePolicy: false,
}));
app.set('trust proxy', 1);
app.use(
rateLimiter({
windowMs: 15 * 60 * 1000,
max: 100,
})
);
app.use(express.json());
const port = process.env.PORT || 5001;
console.log(process.env.NODE_ENV);
const dirname = path.dirname(fileURLToPath(import.meta.url));
app.use(express.static(path.join(dirname, 'build')));
app.get('/', (req, res) => {
res.sendFile(path.join(dirname, 'build', 'index.html'));
});
// app.get('/', (req, res) =>
// res.send('<h1>Camping v2</h1><a href="/docs">Docs</a>')
// );
app.use('/docs', swagger.serve, swagger.setup(swaggerDoc));
app.get('/ping', (req, res) => {
console.log('ping');
res.send();
});
app.use(
auth({
users: { harsh: process.env.PASSWORD },
unauthorizedResponse: { error: 'lol fuck off' },
})
);
app.get('/login', (req, res) => {
res.send();
});
app.use('/parks', parksRouter);
app.use(errorHandler);
// setInterval(() => {
// https.get('https://aqueous-temple-20353.herokuapp.com/ping');
// }, 540000); TODO
const start = async () => {
try {
await mongoose.connect(process.env.MONGO_URI);
// await reschedule(); TODO
app.listen(port, console.log(`Listening on port ${port}`));
} catch (error) {
console.error(error);
}
};
start();