-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
38 lines (30 loc) · 973 Bytes
/
index.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
import express, { Express, NextFunction, Request, Response } from 'express';
import "./src/dbs/initDb";
import router from './src/routers/index.router';
require('dotenv').config();
const app: Express = express();
const port = process.env.PORT || 2000;
app.use(express.json())
app.use(express.urlencoded({
extended: true
}))
app.use('/v1/api', router)
app.get('/', (req: Request, res: Response) => {
res.send('Express + TypeScript Server');
});
app.use((req: Request, res: Response, next: NextFunction) => {
const error : any = new Error('Not found')
error.status = 404
next(error)
})
app.use((error: any, req: Request, res: Response, next: NextFunction) => {
const statusCode = error.status || 500
return res.status(statusCode).json({
status: 'error',
code: statusCode,
message: error.message
})
})
app.listen(port, () => {
console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
});