adding global error handlers
This commit is contained in:
+11
-4
@@ -16,10 +16,20 @@ app.use(morgan('combined'));
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
|
||||
console.error(err);
|
||||
next(err);
|
||||
});
|
||||
|
||||
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
|
||||
res.status(500);
|
||||
res.render('error', { error: err });
|
||||
});
|
||||
|
||||
app.get('/', (req: Request, res: Response) => {
|
||||
res.send('hello world.');
|
||||
});
|
||||
app.use('/api/status', (req: Request, res: Response) => {
|
||||
app.get('/api/status', (req: Request, res: Response) => {
|
||||
res.json({ message: 'wohoo, server is healthy' });
|
||||
});
|
||||
|
||||
@@ -28,9 +38,6 @@ const server = app.listen(PORT, () =>
|
||||
console.log(`express running on port | ${PORT} in host machine`),
|
||||
);
|
||||
|
||||
// won't catch any errors here and we don't want it to
|
||||
console.log();
|
||||
|
||||
const io = require('socket.io')(server, {
|
||||
// todo: add authentication
|
||||
cors: {
|
||||
|
||||
Reference in New Issue
Block a user