-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
127 lines (110 loc) · 4.24 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
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
require("dotenv").config({ path: '.env' });
const express = require('express');
const cors = require('cors');
const fs = require('fs');
const app = express();
const path = require('path');
const mongoose = require('mongoose');
const Place = require('./models/places');
const filePath = path.join(__dirname, 'emails.json');
//connect to mongodb
//const dbURL = "mongodb+srv://tamannajain:[email protected]/NewYork?retryWrites=true&w=majority&appName=Places";
mongoose.connect(process.env.MONGO_URL)
.then((result) => console.log('connected to db'))
.catch((err) => console.error('Failed to connect to db', err));
// Serve files from the 'public' directory
app.use(express.static('public'));
app.use(express.json({ limit: '1mb' }));
app.use(cors());
const port = process.env.PORT || 3000
app.listen(port, () => {
console.log(`Listening at ${port}`)
});
app.post('/api', (request, response) => {
const newEmail = request.body;
console.log(newEmail);
fs.readFile(filePath, (err, data) => {
let emails = [];
if (err) {
console.error('Error reading file:', err);
// Initialize file with empty array if it doesn't exist
fs.writeFile(filePath, JSON.stringify([], null, 2), writeErr => {
if (writeErr) {
console.error('Error initializing file:', writeErr);
response.status(500).json({ message: "Failed to initialize data file" });
return;
}
});
} else if (data.length === 0) {
// Handle empty file scenario
emails = [];
} else {
try {
emails = JSON.parse(data.toString());
} catch (parseErr) {
console.error('Error parsing JSON:', parseErr);
response.status(500).json({ message: "Error parsing data file" });
return;
}
}
emails.push(newEmail);
fs.writeFile(filePath, JSON.stringify(emails, null, 2), (writeErr) => {
if (writeErr) {
console.error('Error writing to file:', writeErr);
response.status(500).json({ message: "Failed to store data" });
} else {
response.json({
message: "Data received at /api",
email: request.body.email
});
}
});
});
});
async function loadFetch() {
const { default: fetch } = await import('node-fetch');
return fetch;
}
app.get('/weather', async (req, res) => {
const fetch = await loadFetch(); // Load fetch dynamically
const apiKey = process.env.WEATHER_API_KEY;
const city = 'New York';
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
try {
const response = await fetch(url);
const data = await response.json();
if (!response.ok) throw new Error(`Failed to fetch weather data: ${response.statusText}`);
res.json(data);
} catch (error) {
console.error('Error fetching weather data:', error);
res.status(500).json({ error: "Error fetching weather data" });
}
});
// POST endpoint to create a new place
app.post('/places', (req, res) => {
const newPlace = new Place({
"title": "Empire State Building",
"description": "A 102-story Art Deco skyscraper in Midtown Manhattan. An iconic symbol of New York City.",
"location": "350 Fifth Avenue, Manhattan, NY 10118",
"rating": 4.7,
"imageUrl": "https://media.tacdn.com/media/attractions-splice-spp-674x446/12/2e/1c/fe.jpg",
});
newPlace.save()
.then(results => {
console.log('Place saved:', results);
res.status(201).json(results); // Send back the created place
})
.catch(error => {
console.error('Error saving place:', error);
res.status(500).json({ message: "Failed to save the place" });
});
});
app.get('/list-places', async (req, res) => {
try {
const places = await Place.find(); // Use Mongoose to find all places
console.log(places)
res.json(places);
} catch (error) {
console.error('Failed to retrieve places:', error);
}
});