-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
62 lines (48 loc) · 1.39 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require('debug-trace')({ always: true })
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const logger = require('morgan');
const app = express();
var pm2 = require('pm2');
app.use(logger(':method :url :status :res[content-length] - :response-time ms'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
/// *** To auto restart the server after the 70 usage
pm2.describe('www',(err,proc)=>{
if(proc[0] && proc[0].monit.cpu>70){
console.log(proc[0].monit.cpu)
console.info("pm2 restarted")
pm2.restart('www', (err, proc) => {
console.info("pm2 restarted")
})
}
})
app.use("/api/v1", require('./app/routes/apis'))
// catch 404 and forward to error handler
app.use(function (req, res, next) {
res.status(404).json({
message:"Not Found"
})
});
// error handler
app.use(function (err, req, res, next) {
// // set locals, only providing error in development
// res.locals.message = err.message;
// res.locals.error = req.app.get('env') === 'development' ? err : {};
// // render the error page
console.log(err)
if (err.message == "UNAUTHORIZED_ACCESS") {
res.status(401).json({
status:false,
message:"user not logged in"
})
}
else{
res.status(500).json({
status:false,
message:"Something Unexpected"
})
}
});
module.exports = app;