-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
44 lines (37 loc) · 1022 Bytes
/
app.ts
File metadata and controls
44 lines (37 loc) · 1022 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
41
42
43
44
import express from 'express';
import morgan from 'morgan';
import mongoose from 'mongoose';
import bodyParser from 'body-parser';
import cors from 'cors';
let app = express();
import { default as api } from './routes/api';
import { NextFunction } from 'express-serve-static-core';
mongoose.connect('mongodb://localhost:27017/user');
app.locals.mongoose = mongoose;
app.listen(3000, () => {
console.log('Listening!');
});
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
app.use(cors());
app.use(morgan('dev'));
app.use('/api', api);
function errorHandler(err: Error | any, req: any, res: any, next: NextFunction) {
if (err.name === 'UnauthorizedError') {
res.status(401).send({
status: 'error', info: {
message: 'Invalid Token!'
}
});
}
else {
res.status(500).send({
status: 'error',
info: {
message: err
}
});
}
}
app.use(errorHandler);
export default app;