-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
40 lines (30 loc) · 921 Bytes
/
server.js
File metadata and controls
40 lines (30 loc) · 921 Bytes
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
39
40
import express from 'express';
import routes from './routes/index.js';
import morgan from 'morgan';
import cors from 'cors';
import jsonWT from './utilities/jwt.js';
const app = express();
app.use(morgan('dev'));
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cors());
// use JWT auth to secure the api
app.use(jsonWT());
app.use(routes);
//error handler
app.use((err, req, res, next) => {
console.error(err);
if (typeof (err) === 'string') {
// custom application error
return res.status(400).json({ message: err });
}
if (err.name === 'UnauthorizedError') {
// jwt authentication error
return res.status(401).json({ message: 'Invalid Token' });
}
return res.status(500).send(err.message);
});
const port = process.env['PORT'] || 4000;
app.listen(port, () => {
console.log(`app listening on port ${port}`);
});