-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
131 lines (112 loc) · 3.53 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require('dotenv').config()
const express = require('express')
const morgan = require('morgan')
const cookieParser = require('cookie-parser')
const createError = require('http-errors')
const bodyParser = require('body-parser')
const cors = require('cors')
const helmet = require('helmet')
const hpp = require('hpp')
var winston = require('./config/winston')
const app = express()
const PORT = process.env.PORT || 5000;
const adminRouter = require('./router/adminRouter');
const dbConn = require('./utils/connect');
const emailer = require('./utils/mail');
const Formidable = require('formidable');
const cron = require("node-cron");
const cloudConfig = require('./config/cloudinary');
const cloudinary = require("cloudinary");
cloudinary.config({
cloud_name: cloudConfig.cloud_name,
api_key: cloudConfig.api_key,
api_secret: cloudConfig.api_secret
});
app.use(cors());
app.use(bodyParser.json({ limit: '200mb' }))
// app logger (for debugging)
morgan.token('data', (req, res) => {
return JSON.stringify(req.body)
})
// logging to log file
app.use(
morgan(
':remote-addr - :remote-user [:date[clf]] ":method :url" :status :res[content-length] ":referrer" ":user-agent" :data',
{ stream: winston.stream }
)
)
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(helmet())
app.use(hpp())
app.get('/test', (req, res) => {
return res.status(200).json({
msg: 'Working perfectly'
})
})
// ADMIN ROUTE
app.use('/admin', adminRouter)
// UPLOAD RESUME ROUTE
app.post('/upload', (req, res, next) => {
// parse a file upload
const form = new Formidable();
console.log('form ', form)
form.parse(req, (err, field, files) => {
cloudinary.uploader.upload(files.upload.path, result => {
console.log(result)
if (result.public_id) {
return res.json({
url: result.url
})
}
}
);
});
})
// Scheduled tasks to be run on the server.
const job = cron.schedule('* * * * *', () => {
console.log('running a task every 1 minute');
// schedule interview reminder
let today = new Date();
let dd = String(today.getDate()).padStart(2, '0');
let mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
let yyyy = today.getFullYear();
let d = new Date();
let hr = d.getHours();
let mn = d.getMinutes();
let ss = d.getSeconds();
today = yyyy + '-' + mm + '-' + dd;
currentTime = hr + ":" + mn + ":" + ":" + ss;
let query = `SELECT * FROM schedule AS sc WHERE date = "${today}"
AND (SELECT TIMEDIFF(sc.start, "${currentTime}") = "00:10:00")`;
dbConn.query(query, (err, data) => {
// to access RowDataPacket
data = JSON.parse(JSON.stringify(data))
// iterate over today's interviews
data.forEach((schedule) => {
console.log('schedule ', schedule);
let sub = "Interview reminder";
emailer.sendEmail(schedule, sub);
})
})
});
// stop the cron-job
// job.stop();
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404, "route doesn't exist"))
})
// error handler
app.use(function (err, req, res, next) {
// To include winston logging (Error)
winston.error(
`${err.status || 500} - ${err.message} - ${req.originalUrl} - ${req.method} - ${req.ip} - ${req.body}`
)
// render the error page
res.status(err.status || 500)
next()
})
app.listen(PORT, () => {
console.log(`Listening on port :: ${PORT}`);
})