init
This commit is contained in:
24
src/main.ts
Normal file
24
src/main.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import express from 'express';
|
||||
import { config } from 'dotenv';
|
||||
|
||||
import { errorHandle } from './utils/error-handling';
|
||||
|
||||
config();
|
||||
|
||||
const app = express();
|
||||
|
||||
const port = process.env.PORT;
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.send('hello')
|
||||
})
|
||||
|
||||
app.get('/error', (req, res) => {
|
||||
throw new Error('some mistake')
|
||||
})
|
||||
|
||||
|
||||
app.use(errorHandle);
|
||||
app.listen(port, () => {
|
||||
console.log(`listen on http://localhost:${port}`);
|
||||
});
|
||||
5
src/utils/error-handling.ts
Normal file
5
src/utils/error-handling.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export const errorHandle = (error, req, res, next) => {
|
||||
res.status(500).send({
|
||||
error: error.message || 'some error'
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user