-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (63 loc) · 1.81 KB
/
index.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const express = require('express');
const app = express();
const dotenv = require('dotenv').config();
const port = process.env.PORT;
const authRoute = require('./routes/auth.routes');
const postRoute = require('./routes/posts.routes');
const interestRoute = require('./routes/interest.routes');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(authRoute);
app.use(postRoute);
app.use(interestRoute);
const customExpress = Object.create(express().response, {
data: {
value(data, status = true) {
return this.type('json').json({
status,
data,
})
}
},
error: {
value(error, message = 'An error occured', code) {
return this.status(code || 500).json({
message,
statusCode: -3,
status: false,
error,
})
}
},
errorMessage: {
value(message = 'API response message', code) {
return this.status(code || 400).json({
message,
statusCode: 1,
status: false,
});
},
},
});
app.response = Object.create(customExpress);
app.use((err, req, res, next) => {
console.error(err);
if (err.type && err.type === 'entity.parse.failed') {
res.status(400).errorMessage('Invalid JSON payload passed.');
} else if (err.toString() === '[object Object]') {
try {
res.status(400).error(err);
} catch {
res.status(500).error('Server error');
}
} else {
res.status(400).error(err.toString());
}
});
app.get('/online', (req, res) => {
res.data('app is online');
});
app.listen(port, () => {
console.log(`app is running on port ${port}`);
})
module.exports = app;