Answer the question
In order to leave comments, you need to log in
How to display data from Rest API server on html page?
I need to display info cards by pulling data from one server, store it on my own and display it on the page.
I created a server and an HTML page, registered taking data, adding elements to the page, but I can’t understand what is missing so that the cards are added to the DOM.
server
const express = require('express');
const path = require('path');
const app = express();
const server = require('http').Server(app);
const fetch = require("node-fetch");
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const url = 'https://reqres.in/api/users';
global.document = new JSDOM(this.html).window.document;
async function getCards() {
let response = await fetch(url);
let cards = await response.json();
let cardsList;
document.addEventListener("DOMContentLoaded", function(event) {
cardsList = document.querySelector('.cards-list');
for (key in cards) {
cardsList.innerHTML += `
<div class="card w-75">
<img src="${content[key].avatar}" class="card-img-top" alt="...">
<div class="card-body">
<p class="email">${content[key].email}</p>
<p class="first-name">${content[key].first-name}</p>
<p class="last-name">${content[key].last-name}</p>
</div>
</div>
`
}
});
}
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'index.html'))
});
server.listen(3000, (err) =>{
if(err){
throw Error(err);
}
console.log('Server is runnig');
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Business Cards</title>
</head>
<body>
<nav class="navbar navbar-light bg-light">
<div class="container-fluid">
<span class="navbar-brand mb-0 h1">Business Cards</span>
</div>
</nav>
<div class="cards-list">
<!-- тут должны быть карточки-->
</div>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question