-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
60 lines (50 loc) · 1.59 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
import express, { NextFunction, Request, Response } from 'express';
import createError from 'http-errors';
import path from 'path';
import cookieParser from 'cookie-parser';
import logger from 'morgan';
import { baseRouter, articlesRouter, testRouter } from './src/routes';
import cors from 'cors';
import dotenv from 'dotenv';
dotenv.config();
const app = express();
/** EXPRESS SETUP */
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
/** CORS */
const allowedOrigins = [
process.env.DEV_CLIENT_BASE_URL as string,
process.env.PROD_CLIENT_BASE_URL as string,
];
const corsOptions: cors.CorsOptions = {
origin: allowedOrigins,
};
app.use(cors(corsOptions));
/** ROUTES */
app.use('/', baseRouter);
app.use('/articles', articlesRouter);
app.use('/test', testRouter);
/** ERROR HANDLING */
// 404
app.use(function (req: Request, res: Response, next: NextFunction) {
next(createError(404));
});
// Catch all
app.use(function (err: any, req: Request, res: Response) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.status(err.status || 500);
res.json('error');
});
/** SERVER SETUP */
app.set('port', process.env.PORT || 6001);
app.listen(app.get('port'), () => {
console.log(
`Express server listening on port ${process.env.PORT} in ${process.env.NODE_ENV} mode.`,
);
console.log(`Allowed origin: ${allowedOrigins}`);
});