A
A
Anastasia2021-07-21 18:57:48
Node.js
Anastasia, 2021-07-21 18:57:48

How to add HTML code from cardsData (on server) to HTML page?

How to add part of the HTML code that is stored in the cardsData variable in index.html?

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;
global.window = new JSDOM(this.html).window;

var cardsData;


async function getCards() {
    await fetch('https://reqres.in/api/users')
    .then(response => response.json())
    .then(json => {
        cardsData = json.data.map(el => {
            return `
                <div class="col col--hidden" >
                    <div class="card">
                        <img src="${el.avatar}" class="card-img-top"  width="200" height="200" alt="...">
                        <div class="card-body">
                            <h5 class="card-title">${el.first_name} ${el.last_name}</h5>
                            <p class="card-text">${el.email}</p>
                        </div>
                    </div>
                </div>
            `;
        });
    });
}

app.get('/', (req, res) => {
    res.sendFile(path.resolve('index.html'));
    getCards();
});

server.listen(3000, (err) =>{
    if(err){
        throw Error(err);
    }
    console.log('Server is runnig');
});


index.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="card-group justify-content-center">
        <div id="cards" class="row row-cols-2 row-cols-md-3"></div>
    </div>
    <a href="#" id="loadMore" class="loadMore"> Load More </a>

    <footer>
        <div class="text-center p-4" style="background-color: rgba(0, 0, 0, 0.05);">
            © Nimchenko Anastasiya 2021
        </div>
    </footer>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        $( document ).ready(function() {
            $(function(){
                $("#loadMore").on('click', function(e){
                    e.preventDefault();
                    $(".col--hidden").slice(0,3).slideDown().removeClass('col--hidden');

                    $(this).toggle();
                })})
        });
    </script>
   
</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 question

Ask a Question

731 491 924 answers to any question