-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
67 lines (56 loc) · 1.93 KB
/
main.ts
File metadata and controls
67 lines (56 loc) · 1.93 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
import express from "express";
import mongoose from "mongoose";
import dotenv from "dotenv";
import cors from "cors";
import userRouter from "./src/routers/user";
import securityRouter from "./src/routers/security";
import emailRouter from './src/routers/email';
import session from "express-session";
import MongoStore from "connect-mongo";
import https from "https";
import { readFileSync } from "fs";
import path from "path";
dotenv.config();
const app = express();
const mongoStore = MongoStore.create({
mongoUrl: process.env.MONGO_URL,
collectionName: 'sessions',
ttl: 60 * 60 * 24 * 14,
autoRemove: 'native'
});
const startup = async () => {
console.log('Mongoose database has been connected.');
app.use(express.json())
.use('/' + process.env.PROFILE_UPLOAD_PATH, express.static(process.env.PROFILE_UPLOAD_PATH as string))
.use(express.urlencoded({ extended: true }))
.use(cors({
credentials: true,
origin: process.env.ORIGIN_URL
}))
.use(session({
name: '_sid',
secret: process.env.SECRET as string,
resave: false,
saveUninitialized: false,
unset: 'destroy',
store: mongoStore,
cookie: {
secure: true,
httpOnly: true,
sameSite: 'none',
maxAge: 1000 * 60 * 60 * 24 * 14,
}
}));
app.use('/api/v1/', userRouter, securityRouter, emailRouter);
https.createServer({
cert: readFileSync('./cert.pem'),
key: readFileSync('./cert-key.pem'),
}, app).listen(process.env.PORT, undefined, () => {
console.log(`Elixir backend is listening on port ${process.env.PORT}.`);
});
}
console.log('Starting backend...');
mongoose.connect(process.env.MONGO_URL as string, {
connectTimeoutMS: 3000,
serverSelectionTimeoutMS: 5000,
}).then(startup).catch(console.log);