-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
78 lines (61 loc) · 2.03 KB
/
server.ts
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
import mongoose from "mongoose";
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const winston = require('winston');
const compression = require('compression');
class Server {
private app: any;
constructor() {
this.app = express();
this.initialieMiddlewares();
this.connectDatabase();
this.setupRoutes();
}
private initialieMiddlewares() {
this.app.use(cors({
origin: process.env.ALLOWED_ORGINS?.split(',') || '*',
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization']
}));
this.app.use(helmet());
const limiter = rateLimit({
windowMs: 15 * 60 *1000,
max: 100,
message: 'Too many requests, please try again later',
standardHeaders: true,
legacyHeaders: false
});
this.app.use(limiter);
this.app.use(express.json({ limit: '10mb' }));
}
private async connectDatabase() {
try {
await mongoose.connect(process.env.MONGO_URI {
retryWrites: true,
w: 'majority'
});
console.log('MongoDB connected succesfully');
} catch (error) {
console.log('MongoDB connection error', error);
process.exit(1);
}
}
private setupRoutes () {
this.app.get('/', (req: any, res: { status: (arg0: number) => { (): any; new(): any; json: { (arg0: { message: string; }): void; new(): any; }; }; }) => {
res.status(200).json({ message: 'Video content backend'})
});
this.app.use(routes);
}
public start(port: number) {
const server = this.app.listen(port, () =>{
console.log(`Server is running on port ${port}`);
});
return server;
}
public getApp() {
return this.app;
}
}
export default Server;