-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapp.js
38 lines (26 loc) · 852 Bytes
/
app.js
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 from 'express';
import dotenv from 'dotenv';
import { router as apiRouter } from './api.js';
dotenv.config();
const {
PORT: port = 3000,
} = process.env;
const app = express();
app.use(express.json());
app.use(apiRouter);
function notFoundHandler(req, res, next) { // eslint-disable-line
console.warn('Not found', req.originalUrl);
res.status(404).json({ error: 'Not found' });
}
function errorHandler(err, req, res, next) { // eslint-disable-line
console.error(err);
if (err instanceof SyntaxError && err.status === 400 && 'body' in err) {
return res.status(400).json({ error: 'Invalid json' });
}
return res.status(500).json({ error: 'Internal server error' });
}
app.use(notFoundHandler);
app.use(errorHandler);
app.listen(port, () => {
console.info(`Server running at http://127.0.0.1:${port}/`);
});