adding global error handlers
This commit is contained in:
+11
-4
@@ -16,10 +16,20 @@ app.use(morgan('combined'));
|
|||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(express.json());
|
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) => {
|
app.get('/', (req: Request, res: Response) => {
|
||||||
res.send('hello world.');
|
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' });
|
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`),
|
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, {
|
const io = require('socket.io')(server, {
|
||||||
// todo: add authentication
|
// todo: add authentication
|
||||||
cors: {
|
cors: {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"ignore": ["**/*.test.ts", "**/*.spec.ts", "node_modules"],
|
"ignore": ["**/*.test.ts", "**/*.spec.ts", "node_modules"],
|
||||||
"watch": ["./", "../common"],
|
"watch": ["./", "../core"],
|
||||||
"exec": "npm run start:dev",
|
"exec": "npm run start:dev",
|
||||||
"ext": "ts"
|
"ext": "ts"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"core": "rm -rf ./core && cp -r ../core/src ./core/",
|
"core": "rm -rf ./core && cp -r ../core/src ./core/",
|
||||||
"dev": "NODE_ENV=development && yarn core && nodemon",
|
"dev": "NODE_ENV=development nodemon",
|
||||||
"start:dev": "ts-node index.ts",
|
"start:dev": "ts-node index.ts",
|
||||||
"lint": "eslint --ext .ts,.tsx .",
|
"lint": "eslint --ext .ts,.tsx .",
|
||||||
"build": "npm run lint && tsc -p .",
|
"build": "npm run lint && tsc -p .",
|
||||||
|
|||||||
Reference in New Issue
Block a user