-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
84 lines (64 loc) · 1.95 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
74
75
76
77
78
79
80
81
82
83
84
import express from 'express';
import dotenv from 'dotenv';
import { scrapeVulnerabilities } from "./controllers/scraperController.cjs";
import mongoose from 'mongoose';
import cors from "cors";
import userRoutes from "./routes/userRoutes.cjs";
import cron from 'node-cron';
import vulnerabilityModel from './models/vulnerabilityModel.cjs';
import { sendEmail } from './services/mail.cjs';
dotenv.config();
const app = express();
app.use(express.json());
app.use(cors());
const data = {
MessageChannel : 'message-channel',
navigator : 'https',
};
app.get('/json', (req, res) => {
console.log('hh')
res.status(200).json(data);
});
const connectDb = async () => {
try {
await mongoose.connect(process.env.MONGO_URI);
console.log("Server connected to db");
} catch (error) {
console.log("Server not connected to db");
}
};
connectDb();
app.use('/api', userRoutes);
app.get('/vulnerabilities', async (req, res) => {
try {
const vulnerabilities = await vulnerabilityModel.find();
res.json(vulnerabilities);
} catch (err) {
res.status(500).json({ error: 'Error fetching vulnerabilities' });
}
});
// Scraping Example
// scheduler for every 10 minutes
// cron.schedule('*/10 * * * *', async () => {
// console.log('Running scraper...');
// await scrapeVulnerabilities();
// });
// cron.schedule('0 6 * * *', async () => {
// console.log('Running scraper...');
// const vulnerabilities = await scrapeVulnerabilities();
// vulnerabilities.then(data =>
// {
// console.log('Vulnerabilities from index: ',data);
// sendEmail(data);
// }
// )
// .catch(err => console.log('Error: ',err));
// });
cron.schedule('0 6 * * *', async () => {
console.log('Running scraper...');
const vulnerabilities = await scrapeVulnerabilities();
await sendEmail();
});
app.get('/test', async () => {sendEmail()});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));