-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
113 lines (94 loc) · 2.86 KB
/
server.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
const express = require('express');
const mongoose = require('mongoose');
const nodemailer = require('nodemailer');
const ExcelJS = require('exceljs');
const cron = require('node-cron');
const app = express();
app.use(express.json());
// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/websiteData', { useNewUrlParser: true, useUnifiedTopology: true });
const visitorSchema = new mongoose.Schema({
timestamp: { type: Date, default: Date.now },
type: String,
url: String
});
const Visitor = mongoose.model('Visitor', visitorSchema);
// Track visitor
app.post('/track', async (req, res) => {
const { type, url } = req.body;
const visitor = new Visitor({ type, url });
await visitor.save();
// Send email
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'your-email-password'
}
});
const mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'New Visitor Data',
text: `Type: ${type}, URL: ${url}, Timestamp: ${new Date()}`
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
res.sendStatus(200);
});
// Get visitor count for today
app.get('/visitorCount', async (req, res) => {
const startOfDay = new Date();
startOfDay.setHours(0, 0, 0, 0);
const count = await Visitor.countDocuments({ timestamp: { $gte: startOfDay } });
res.json({ count });
});
// Generate and send monthly report
cron.schedule('0 0 1 * *', async () => {
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('Monthly Report');
worksheet.columns = [
{ header: 'Timestamp', key: 'timestamp', width: 30 },
{ header: 'Type', key: 'type', width: 10 },
{ header: 'URL', key: 'url', width: 50 }
];
const visitors = await Visitor.find({});
visitors.forEach(visitor => {
worksheet.addRow(visitor);
});
const buffer = await workbook.xlsx.writeBuffer();
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'your-email-password'
}
});
const mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Monthly Visitor Report',
text: 'Please find the attached monthly visitor report.',
attachments: [
{
filename: 'Monthly_Report.xlsx',
content: buffer
}
]
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});