-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (43 loc) · 1.51 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
const express = require('express');
const cors = require('cors');
const bodyParser = require("body-parser")
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const fetchingAPI = require('./api/fetchAPI');
const apikey = '48ebf7308a22ff1e72288dc068d74de9';
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//app.use(cors());
app.use(bodyParser.json());
// Add Access Control Allow Origin headers
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
app.post('/CurrentWeather', (req, res) => {
const {cityName} = req.body;
const apiUrl = `http://api.openweathermap.org/geo/1.0/direct?q=${cityName}&limit=5&appid=${apikey}`;
fetchingAPI.fetchAPI(apiUrl, 'weather', apikey, res);
});
// Add Access Control Allow Origin headers
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
app.post('/FiveDayForecast', (req, res) => {
const {cityName} = req.body;
const apiUrl = `https://api.openweathermap.org/geo/1.0/direct?q=${cityName}&limit=5&appid=${apikey}`;
fetchingAPI.fetchAPI(apiUrl, 'forecast', apikey, res);
});
app.listen(PORT, () =>{
console.log(`App is running on port ${PORT}`);
});