-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
executable file
·56 lines (46 loc) · 1.36 KB
/
app.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
const path = require('path');
import express, { Request, Response } from 'express';
import cors from 'cors';
import cookieParser from 'cookie-parser';
import csurf from 'csurf';
require('dotenv').config();
import AuthRoutes from './routes/authRoutes';
import UserRoutes from './routes/userRoutes';
import { requireAuth } from './middleware/authMiddleware';
import ApiErrorHandler from './error/ApiErrorHandler';
import connectDB from './config/db';
const app = express();
const port = process.env.PORT || 8080;
// middleware
app.use(cors());
app.use(express.json({ limit: '50mb' }));
app.use(cookieParser());
const csrfProtection = csurf({
cookie: {
httpOnly: true
}
});
// db connection
connectDB();
// routes
app.use("/api", AuthRoutes);
app.use(csrfProtection);
app.get('/api/csrf-token', (req: Request, res: Response) => {
//@ts-ignore
res.json({ csrfToken: req.csrfToken() })
});
app.use("/api/user", requireAuth, UserRoutes);
if (process.env.NODE_ENV === 'production') {
app.use("/", express.static(path.join(__dirname, 'client', 'build')));
app.get('*', async (req: Request, res: Response) =>
res.sendFile(
path.resolve(__dirname, 'client', 'build', 'index.html')
)
);
}
// Error Handler
app.use(ApiErrorHandler);
// Start Server
app.listen(port, function () {
console.log("\x1b[36m%s\x1b[0m", "Server up and running on port:", port);
});