Skip to content

Commit 239483f

Browse files
committed
Initial commit
0 parents  commit 239483f

File tree

11 files changed

+3029
-0
lines changed

11 files changed

+3029
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.env

index.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import * as dotenv from 'dotenv';
2+
dotenv.config()
3+
import request from 'request';
4+
import express from 'express';
5+
import bodyParser from 'body-parser';
6+
7+
// Create network routing
8+
const app = express();
9+
10+
// EJS is accessed by default in the views directory.
11+
app.set('view engine', 'ejs');
12+
13+
// Allow access to 'public' folder
14+
app.use(express.static('public'));
15+
16+
app.use(bodyParser.urlencoded({ extended: true }));
17+
18+
app.get('/', (req, res) => {
19+
// res.status(200).send("Welcome to the staging app -- current weather conditions.");
20+
res.render('index');
21+
})
22+
23+
app.post('/', (req, res) => {
24+
console.log(req.body.locale);
25+
// call weatherAPI for data
26+
let apiKey = process.env.WEATHER_VISUALCROSSING_API_KEY;
27+
let city = req.body.locale;
28+
let url = process.env.WEATHER_VISUALCROSSING_API_BASE_URI;
29+
30+
/* https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/Fremont%2C%20CA?unitGroup=us&contentType=json&key=E5PZ48U2C8EB6692GBD39U9LC */
31+
let uriStr = `${url}${city}?unitGroup=us&contentType=json&key=${apiKey}`;
32+
console.log(uriStr);
33+
request(uriStr, async function (err, response, body) {
34+
if(err){
35+
console.log('error:', err);
36+
} else {
37+
let weather = await JSON.parse(body).currentConditions;
38+
console.log(weather);
39+
40+
// res.render('index', {locale: city, data: weather, error: null});
41+
42+
if (weather.datetime == undefined) {
43+
console.log("Something went wrong. No weather data.");
44+
res.render('index', { data: null, error: 'Error, please check your input.'});
45+
} else {
46+
console.log("currentConditions:\n");
47+
console.log(weather);
48+
res.render('index', { locale: city ,data: weather, error: null });
49+
}
50+
}
51+
});
52+
53+
})
54+
55+
56+
// about page
57+
app.get('/about', function(req, res) {
58+
res.render('about');
59+
});
60+
61+
let port = process.env.PORT || 3210;
62+
63+
// creating a server that is listening on ${port} for connections.
64+
app.listen(port, () => {
65+
console.log(`Staging app is listening on port ${port}`);
66+
});
67+

0 commit comments

Comments
 (0)