-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
52 lines (46 loc) · 1.26 KB
/
server.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
// Server
const express = require("express");
const axios = require("axios");
const cors = require("cors");
const app = express();
const port = 8080;
let corsOptions = {
origin: "*",
methods: "GET, PUT, POST, PATCH",
};
app.use(cors(corsOptions));
app.get("/rockets", (req, res) => {
let rocketId = req.query.rocketId;
let limitValue = req.query.limit;
let offset = req.query.offset;
let url = rocketId
? `https://api.spacexdata.com/v3/rockets/${rocketId}`
: `https://api.spacexdata.com/v3/rockets?offset=${offset}&limit=${limitValue}`;
axios
.get(url)
.then((response) => {
res.send(response.data);
})
.catch((error) => {
console.log(error);
});
});
app.get("/launches", (req, res) => {
let limitValue = req.query.limit;
let flight_number = req.query.flightNumber;
let offset = req.query.offset;
let url = flight_number
? `https://api.spacexdata.com/v3/launches/${flight_number}`
: `https://api.spacexdata.com/v3/launches/past?offset=${offset}&limit=${limitValue}`;
axios
.get(url)
.then((response) => {
res.send(response.data);
})
.catch((error) => {
console.log(error);
});
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});