-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (122 loc) · 4.5 KB
/
index.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
process.stdin.setEncoding("utf8");
const http = require("http");
const express = require("express");
const bodyParser = require("body-parser");
const axios = require("axios");
const path = require("path");
require("dotenv").config({ path: path.resolve(__dirname, ".env") });
const { connect, login, loadPhotos } = require("./database");
const { response } = require("express");
const { get } = require("express/lib/response");
const port = 4000;
const app = express();
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.urlencoded({ extended: false }));
app.set("views", path.resolve(__dirname, "templates"));
app.set("view engine", "ejs");
//global objects
let user;
let array = [];
let thumbnails;
let currentImage;
app.get("/", (req, res) => {
res.render("login");
});
app.post("/login", async (req, res) => {
user = { email: req.body.email, name: req.body.name };
//retrieve or create user
user = await login(user);
res.render("home", user);
});
app.get("/search", (req, res) => {
res.render("search");
});
app.post("/processSearch", async (request, response) => {
let query = request.body.query;
//Clear search results
let searchResults = {
results: [],
};
//Initial query to NASA api
let apiResponse = await axios.get(`https://images-api.nasa.gov/search?q=${query}`);
searchResults["totalResults"] = apiResponse.data.collection.metadata.total_hits;
searchResults["links"] = apiResponse.data.collection.links;
let urls = apiResponse.data.collection.items.map((item) => {
return {
url: item.href,
title: item.data[0].title,
};
});
let counter = 0;
Promise.all(
urls.map((endpoint) =>
axios.get(encodeURI(endpoint.url)).then((urls) => {
let nextResult = {};
//Only create results for images videos (no audio files)
if (urls.data.find((string) => string.includes("thumb."))) {
//set thumbnail image
nextResult["thumb"] = urls.data.find((string) => string.includes("thumb."));
//search for highest quality video/image available
//orig video -> large video -> any video
//orig jpg -> large jpg -> any jpg
let fullSize = urls.data.find((string) => string.includes("orig.mp4"))
? urls.data.find((string) => string.includes("orig.mp4"))
: urls.data.find((string) => string.includes("large.mp4"))
? urls.data.find((string) => string.includes("large.mp4"))
: urls.data.find((string) => string.includes(".mp4"))
? urls.data.find((string) => string.includes(".mp4"))
: urls.data.find((string) => string.includes("orig.jpg"))
? urls.data.find((string) => string.includes("orig.jpg"))
: urls.data.find((string) => string.includes("large.jpg"))
? urls.data.find((string) => string.includes("large.jpg"))
: urls.data.find((string) => string.includes(".jpg"));
nextResult["fullSize"] = fullSize;
//Get title from parent searchResult object
nextResult["title"] = endpoint.title;
searchResults.results.push(nextResult);
}
counter += 1;
})
)
).then(() => {
//Render results
let thumbnails = "";
searchResults.results.forEach((result, index) => {
thumbnails += `<div class="thumb"><a href="/image?index=${index}"><img src="${result.thumb}" id="${index}" alt="${result.title}" /></a>${result.title}</div>`;
});
let buttons = "";
if (searchResults.links) {
searchResults.links.forEach((link) => {
let query = link.href.substring(link.href.indexOf("?q="));
buttons += `<a href="/processSearch/${query}"><button>${link.prompt}</button></a>`;
});
}
response.render("searchResult", { count: searchResults.totalResults, images: thumbnails, navigation: buttons });
});
});
app.get("/processSearch", (request, response) => {
let query = request.originalUrl.substring(request.originalUrl.indexOf("?q=") + 3);
axios({
method: "post",
url: "/processSearch",
data: {
query: query,
},
});
});
//Connect to database
connect();
//Start server
http.createServer(app).listen(port);
process.stdout.write(`Web server started and running at http://localhost:${port}\n`);
process.stdin.on("readable", function () {
let dataInput = process.stdin.read();
if (dataInput !== null) {
let command = dataInput.trim();
if (command === "stop") {
process.stdout.write("Shutting down the server\n");
process.exit(0);
}
process.stdin.resume();
}
});