-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
68 lines (56 loc) · 1.86 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
57
58
59
60
61
62
63
64
65
66
67
68
import express from 'express';
import cors from 'cors';
import morgan from 'morgan';
import cookieParser from 'cookie-parser';
import path from 'path';
import mongodbConnect, { testMongodb } from './mongo';
import config from './utils/config';
import postRouter from './routes/posts';
import userRouter from './routes/users';
import authRouter from './routes/auth';
import testRouter from './routes/tests';
import likeRouter from './routes/likes';
import notificationRouter from './routes/notifications';
import { errorHandler, logErrorCodes } from './utils/middleware';
const { NODE_ENV } = process.env;
const app = express();
if (NODE_ENV === 'production' || NODE_ENV === 'development') {
const mongodbUri = NODE_ENV === 'production'
? config.PROD_MONGODB_URI
: config.DEV_MONGODB_URI;
if (!mongodbUri) {
throw new Error(`The ${NODE_ENV} MongoDB URI is not defined.`);
}
mongodbConnect(mongodbUri);
} else if (NODE_ENV === 'cypress') {
(async () => {
await testMongodb.connect();
})();
}
app.use(logErrorCodes);
app.get('/health', (_req, res) => {
res.send('ok');
});
app.use(cookieParser());
app.use(cors());
app.use(express.static('build'));
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: true }));
app.use(morgan('dev'));
app.use('/api/posts', postRouter);
app.use('/api/users', userRouter);
app.use('/api/auth', authRouter);
app.use('/api/likes', likeRouter);
app.use('/api/notifications', notificationRouter);
if (NODE_ENV === ('production' || 'development')) {
app.get('*', (_req, res) => {
const prodPath = path.join(__dirname, 'index.html');
const devPath = path.join(__dirname, '..', 'build', 'index.html');
res.sendFile(NODE_ENV === 'production' ? prodPath : devPath);
});
}
if (NODE_ENV !== 'production') {
app.use('/api/test', testRouter);
}
app.use(errorHandler);
export default app;