Add index image results, improve routes

main
Elnu 2 years ago
parent 25a813097c
commit 45bcde3b79

@ -22,23 +22,29 @@ async function getResults(query) {
});
}
app.get("/random/:query*.jpg", (request, response) => {
app.get(["/random/:query*.jpg" /* legacy routing */, "/:query/random.jpg", "/:query/random"], (request, response) => {
getResults(request.params.query).then(results => {
response.redirect(results[Math.floor(Math.random() * results.length)]);
});
});
app.get("/:query*.jpg", (request, response) => {
app.get("/:query/list", (request, response) => {
response.writeHead(200, { "Content-Type": "application/json" });
getResults(request.params.query).then(results => {
response.redirect(results[0]);
response.write(JSON.stringify(results));
response.end();
});
});
app.get("/:query", (request, response) => {
response.writeHead(200, { "Content-Type": "application/json" });
app.get(["/:query/:i.jpg", "/:query/:i"], (request, response) => {
getResults(request.params.query).then(results => {
response.write(JSON.stringify(results));
response.end();
response.redirect(results[request.params.i % results.length]);
});
});
app.get(["/:query.jpg", "/:query"], (request, response) => {
getResults(request.params.query).then(results => {
response.redirect(results[0]);
});
});