-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
79 lines (65 loc) · 2.94 KB
/
server.js
File metadata and controls
79 lines (65 loc) · 2.94 KB
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
require('dotenv').config();
const express = require('express');
const nodemailer = require('nodemailer');
const cors = require('cors');
const axios = require('axios'); // For fetching weather data
const app = express();
const port = 5000;
app.use(express.json());
app.use(cors());
// Weather API Key (from OpenWeatherMap)
const WEATHER_API_KEY = process.env.WEATHER_API_KEY;
// Nodemailer Configuration for Sending Emails
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});
// ✅ Emergency Contacts + Weather Alerts
app.get('/api/emergency-contacts', async (req, res) => {
const userLocation = req.query.city || "New York"; // Default city if not provided
try {
// Fetch current weather data
const weatherResponse = await axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${userLocation}&appid=${WEATHER_API_KEY}&units=metric`);
const weatherData = weatherResponse.data;
// Extract weather details
const weatherCondition = weatherData.weather[0].main; // e.g., Rain, Storm, Clear
const temperature = weatherData.main.temp;
const city = weatherData.name;
// Emergency contacts
const contacts = [
{ name: "City Shelter", type: "Shelter", phone: "7411254552" },
{ name: "General Hospital", type: "Hospital", phone: "7411254552" },
{ name: "Rescue Team 24/7", type: "Rescue", phone: "9353519844" }
];
// Check for dangerous weather conditions
if (weatherCondition === "Rain" || weatherCondition === "Storm" || weatherCondition === "Extreme") {
const alertMessage = `🚨 WEATHER ALERT: ${weatherCondition} detected in ${city}. Temperature: ${temperature}°C. Stay safe!`;
// Send Alert Email
const mailOptions = {
from: process.env.EMAIL_USER,
to: "harshithaan2004@gmail.com@example.com", // Change this to the actual user's email
subject: "🚨 Severe Weather Alert!",
text: alertMessage,
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error("Error sending email:", error);
} else {
console.log("Weather alert email sent: " + info.response);
}
});
}
// Send emergency contacts + weather response
res.status(200).json({ city, temperature, weatherCondition, contacts });
} catch (error) {
console.error("Error fetching weather data:", error);
res.status(500).json({ success: false, message: "Failed to fetch weather data" });
}
});
// Start Server
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});