-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
173 lines (149 loc) · 5.3 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//import and set up environment modules
import dotenv from 'dotenv'
dotenv.config()
//import packages
import express, { request } from 'express'
import fs from 'fs'
import { MongoClient, CURSOR_FLAGS, ObjectId } from 'mongodb'
import fetch from 'node-fetch';
//create app
const app = express();
//get port from environment variable or user localhost:3000
const port = process.env.PORT || 3000
//start listening
app.listen(port, () => console.log(`Listening at port: ${port}`))
//use the express library to host static files
app.use(express.static('public'));
//enable ability to receive json data
app.use(express.json({limit: '1mb'}));
//uncomment to use local uri
// const db_uri = 'mongodb://127.0.0.1:27017'
//use mongodb atlas
const db_uri = `mongodb+srv://${process.env.DB_USER}:${process.env.PASSWORD}@cluster0.b8ua4.mongodb.net/location-app.?retryWrites=true&w=majority`
//new instance of mongo client
const client = new MongoClient(db_uri, { useNewUrlParser: true, useUnifiedTopology: true });
//add endpoints
app.get('/api', async (request, response) => {
//access info from db and send to response
const query = ""
findDb(response, query, "weather", "location-app");
});
app.get('/weather/:lat/:lon', async (request, response) => {
//receive latlon info from client and use it to get weather +aq data
//from external APIs
const lat = request.params.lat;
const lon = request.params.lon;
const APIkey = process.env.API_KEY
//use openweather API
const weatherUrl = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${APIkey}`
const weatherResponse = await fetch(weatherUrl);
const weatherData = await weatherResponse.json();
//use open air quality API
const aqUrl = `https://u50g7n0cbj.execute-api.us-east-1.amazonaws.com/v2/latest?limit=100&page=1&offset=0&sort=desc&coordinates=${lat},${lon}&radius=1000&order_by=lastUpdated&dumpRaw=false`
const aqResponse = await fetch(aqUrl);
const aqData = await aqResponse.json();
const data = {
weather: weatherData,
aq: aqData,
}
response.json(data);
})
app.post('/weather', (request, response) => {
//save weather data to db
response.json({
status: "success",
data: request.body,
})
//update database
const newLocation = {
date: Date.now(),
lat: request.body.latitude,
lon: request.body.longitude,
country: request.body.weather.sys.country,
weather: request.body.weather.weather[0].main,
temp: request.body.weather.main.temp,
feels: request.body.weather.main.feels_like,
humidity: request.body.weather.main.humidity,
aqArray: request.body.aqArray,
}
addOneDb(newLocation, "weather", "location-app")
});
app.post('/delete_location', (request, response) => {
response.json({
status: "success",
info: `Deleted location ID: ${request.body.locationID}`
});
const query = {"_id" : ObjectId(request.body.locationID)};
deleteOneDb(query, "weather", "location-app")
})
// app.post('/api', (request, response) => {
// response.json({
// status: "success",
// data: request.body,
// })
// //updateLocalFile(request)
// updateDb(request, "location")
// });
async function deleteOneDb(query, collectionName, dbName){
try{
//connect to client
await client.connect();
//delete location
console.log(query)
const result = await client.db(dbName).collection(collectionName).deleteOne(query);
console.log(`Update DB: ${dbName}`)
console.log(`Updated collection: ${collectionName}`)
console.log(`${result.deletedCount} ID deleted: ${query._id}`);
} catch (e) {
console.error(e)
} finally {
//close connection after performing all operations
await client.close()
}
}
async function findDb(response, query, collectionName, dbName){
// query data from db
try{
//connect to client
await client.connect();
//list all entries
const cursor = await client.db(dbName).collection(collectionName).find(query);
const data = await cursor.toArray();
//send data
response.json(data);
} catch(e){
console.error(e);
} finally {
await client.close();
}
}
async function addOneDb(newLocation, collectionName, dbName){
//update db with new data
try{
//connect to client
await client.connect();
const result = await client.db(dbName).collection(collectionName).insertOne(newLocation);
console.log(`Update DB: ${dbName}`)
console.log(`Updated collection: ${collectionName}`)
console.log(`New location ID: ${result.insertedId}`);
} catch (e) {
console.error(e)
} finally {
//close connection after performing all operations
await client.close()
}
}
function updateLocalFile(request){
//load and save data to local file
fs.readFile("location.txt", 'utf-8', (err, data) => {
console.log("File loaded")
location = JSON.parse(data)
location.push(request.body)
console.log(location)
fs.writeFile("location.txt", JSON.stringify(location), (err) => {
if (err) throw err;
console.log("File saved")
})
})
}
//nodemon ./server.js