Question
Want the html page to print out each picture stored in my database.db file when making a GET request. I want the array of pictures
Want the html page to print out each picture stored in my database.db file when making a GET request. I want the array of pictures to be stored in a variable called apods, then for each picture run them through the function to print it out when making get request. I have the two database entries in json format, how can I get the url,date and assign them to a variable in script.js?
script.js
(() => {
const makeAPOD = (url, date) => {
var div = document.createElement("div");
div.className = "apod";
var small = document.createElement("small");
small.id = "apod-date";
small.innerText = date;
var img = document.createElement("img");
img.src = url;
img.style.width = "200px";
div.appendChild(small);
div.appendChild(img);
return div;
};
fetch('http://localhost:8080/api/favorite')
.then(response => response.json())
.then(json => {
let apods = [JSON.parse(json)];
var al = document.getElementById("apod-list");
for (apod of apods) {
console.log(apod);
al.appendChild(makeAPOD(apod));
}
});
})();
database.db file
{"date":"2022-01-01","image_url":"https://apod.nasa.gov/apod/image/2201/MoonstripsAnnotatedIG_crop1024.jpg","_id":"hBCNNWB0hp0xEdiz"}
{"date":" 02-21-2021 ","image_url":"https://apod.nasa.gov/apod/image/2102/rosette_goldman_960.jpg","_id":"pU39c5f8bY7pDiKk"}
index.js
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const url = "mongodb://127.0.0.1:27017ode-mongo-hw"; // change this as needed
const Datastore = require('nedb');
const axios = require('axios');
mongoose.connect(url, { useNewUrlParser: true });
const db = mongoose.connection;
db.once("open", (_) => {
console.log("Database connected:", url);
});
db.on("error", (err) => {
console.error("connection error:", err);
});
const app = express();
const database = new Datastore('database.db');
database.loadDatabase();
const cors = require('cors')
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors())
var port = process.env.PORT || 8080;
var router = express.Router();
router.get("/favorite", function (req, res) {
// TODO:
})
});
app.use("/api", router); // API Root url at: http://localhost:8080/api
app.listen(port);
console.log("Server listening on port " + port);
Your Favorite APOD Your Favorite APODStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started