-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
28 lines (21 loc) · 805 Bytes
/
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
import express, { Request, Response, NextFunction} from 'express';
import debugLogger from 'debug';
import booksRouter from './src/routers/booksRouter';
import bodyParser from 'body-parser';
const port = process.env.PORT || 4000;
const server = express();
const debug = debugLogger('app');
server.use(bodyParser.json());
server.use('/api', booksRouter);
//global error handler registered in middleware
//to be registered after all other middleware - last in the middleware chain
server.use((err: Error, req: Request, res: Response, next: NextFunction) => {
debug(err.stack);
res.status(500).json({
error: 'Unable to handle the request at the moment'
});
});
export const listener = server.listen(port, () => {
debug(`Server listening on ${port}`);
});
export default server;