-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (48 loc) · 1.43 KB
/
app.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
// Basic setup
var express = require("express");
var app=express();
var path= require("path")
const hbs = require("hbs");
var port = process.env.PORT || 3000 ;
app.use(express.static(path.join(__dirname, 'src')));
app.use(express.static(__dirname));
// API
const geoCode = require("./src/utils/geoCode.js");
const forecast = require("./src/utils/forecast.js");
//ROUTES
app.get("/",(req,res)=>{
res.sendFile(path.join(__dirname,"/src/index.html"));
});
app.get("/weather",(req,res)=>{
if(req.query.address){
geoCode(req.query.address, (error, geoData)=>{
if(error){return res.send({error:"Error in Geocode"})}
forecast(geoData.latitude, geoData.longitude, (error, forecastData) => {
if(error){return res.send({error:"Error in DarkSky api"});}
res.send({
forecast:forecastData,
location:req.query.address,
data: req.data
});
// res.redirect("http://localhost:3000/weatherform");
});
});
}else{
res.send({
error:"You must provide the location"
});
}
});
app.get("/weatherform",(req,res)=>{
res.sendFile(path.join(__dirname,"/src/weather.html"));
})
app.get("/about",(req,res)=>{
res.sendFile(path.join(__dirname,"/src/About.html"));
});
app.get("*",(req,res)=>{
res.sendFile(path.join(__dirname,"/src/404.html"));
});
//PORT
app.listen(port,()=>{
console.log("radi");
});