-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
52 lines (45 loc) · 1.32 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
import express, { Request, Response, NextFunction } from 'express';
const app = express();
import cors from 'cors';
import { CorsOptions } from 'cors';
import morgan from 'morgan';
import apiRoutes from './api';
require('dotenv').config();
const whiteList = [
process.env.LOCAL_URL,
'http://localhost:5173',
process.env.NGROK_URL,
];
const corsOptions: CorsOptions = {
origin: function (origin, callback) {
if (whiteList.indexOf(origin) !== -1 || !origin) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
methods: 'GET,PUT,PATCH,HEAD,POST,DELETE',
credentials: true,
};
app.use(cors(corsOptions));
app.use(morgan('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/api/v1', apiRoutes);
app.get('/', (req: Request, res: Response) => {
res.status(401).json({
message:
'Hello from Hartbeat TC! Unfortunately, you are not authorized to access this page.',
status: 401,
error: 'Unauthorized',
});
});
// app.use('/');
// error handling endware
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
console.error(err);
console.error(err.stack);
const errStatus = (err as any).status || 500;
res.status(errStatus).send(err.message || 'Internal server error.');
});
export default app;