-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
44 lines (36 loc) · 1.3 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
// Import the required modules
const express = require('express');
// 'request' is a popular and widely used library
// for making HTTP requests from Node.js on the server side.
const request = require('request');
// Initialize the Express app
const app = express();
// Define endpoint for server-side proxy with the 'get' method.
//'proxy' is an arbitrarily chosen name for the endpoint.
app.get('/proxy', (req, res) => {
// Get URL from query
const url = req.query.url;
console.log(url);
// Define custom User-Agent header
const headers = {
'User-Agent': 'weather_tracker-v1.0',
'Referer': 'https://github.com/phillipashford/weather-tracker'
};
// Send request to the outside API endpoint (url) using the request module
request({ url: url, headers: headers }, (error, response, body) => {
if (error) {
// Returns status code if there's an error
res.status(500).send({ error });
} else {
// Returns response from API
res.send(body);
}
});
});
// Checks to see if the port environmental variable has already been defined, and if not
// assigns '3000'. 3000-6000 are commonly assigned port numbers for development purposes.
const port = process.env.PORT || 3000;
// Starts the Express app
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});