-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
97 lines (85 loc) · 2.81 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//Require frameworks and libraries
const express = require("express");
const request = require("request");
var app = express();
//Serve Public directory
app.use(express.static(__dirname + "/public"));
app.set("view engine", "ejs");
var apikey = process.env.apikey;
//index route
app.get("/", (req, res) => {
var url = "https://api.nasa.gov/planetary/apod?api_key="+apikey;
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var apod = JSON.parse(body);
res.render("index", { apod: apod });
}
})
})
app.get("/APOD", (req, res) => {
var query = req.query.search;
var url = "https://api.nasa.gov/planetary/apod?api_key=" + apikey + "&date=" + query;
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var apod = JSON.parse(body);
res.render("index", { apod: apod });
}
})
})
//rover route
app.get("/rover", (req, res) => {
var url = "https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?earth_date=2015-6-3&api_key=" + apikey;
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
res.render("rover", { data: data });
}
})
})
app.get("/rover/sol", (req, res) => {
var query = req.query.sol;
var url = "https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?sol=" + query + "&api_key=" + apikey;
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
res.render("rover", { data: data });
}
})
})
app.get("/rover/earth-date", (req, res) => {
var query = req.query.date;
var url = "https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?earth_date=" + query + "&api_key=" + apikey;
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
res.render("rover", { data: data });
}
})
})
//DSCOVR EPIC ROUTE
app.get("/epic", (req, res) => {
var defaultdate = "2019-01-01";
var date = defaultdate.replace(/-/g, "/");
var url = "https://api.nasa.gov/EPIC/api/natural/date/" + defaultdate + "?api_key=" + apikey;
console.log(url);
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
res.render("epic", { data: data, date: date });
}
})
})
app.get("/epic/pictures", (req, res) => {
var query = req.query.date;
var date = query.replace(/-/g, "/");
var url = "https://api.nasa.gov/EPIC/api/natural/date/" + query + "?api_key=" + apikey;
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
res.render("epic", { data: data, date: date});
}
})
})
app.listen(process.env.PORT || "3000", process.env.IP, () => {
console.log("server running");
})