-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
27 lines (23 loc) · 691 Bytes
/
server.js
File metadata and controls
27 lines (23 loc) · 691 Bytes
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
const express = require("express");
const app = express();
const port = 8080;
const cors = require("cors");
const axios = require("axios");
app.use(cors());
app.get("/", (req, res) => {
res.set("Content-Type", "application/json");
const fetchUrl = req.query.url
? req.query.url
: "https://reddit.com/r/reactjs.json";
axios.get(fetchUrl).then(function (response) {
let data = response.data.data;
res.send(JSON.stringify(data));
}).catch((error) => {
res.status(error.response.data.error);
res.send(error.response.data.message);
});
});
// start the Express server
app.listen(port, () => {
console.log(`server started at http://localhost:${port}`);
});