A
A
Anastasia2021-07-19 19:00:06
Express.js
Anastasia, 2021-07-19 19:00:06

How to redesign a project with a focus on the back-end?

The project is that there is a site with data, I make a request to it to receive data and then display this data on my server. My code is made with a focus on the front-end, please tell me how to remake it so that it is focused on the back-end?

Front-end 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>
    <style>

        html {
            margin: 0 auto;
            margin-left: auto;
            margin-right: auto;
        }

        .col--hidden {
            display: none;
            margin: 5px 5px 5px 5px;
        }
        .row {
            margin-left: auto;
            margin-right: auto;
            align-items: center;
        }
        .card {
            box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
            transition: 0.3s;
            width: 250px;
            height: 300px;
        }
        .card:hover {
            box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
        }

        .card-group {
            margin-top: 40px;
            margin-bottom: 40px;
        }
        .col {
            align-items: center;
            margin-left: auto;
            margin-right: auto;
        }
        #img {
            border-radius: 5px 5px 0 0;
        }

        .loadMore {
            margin-top: 40px;
            margin-bottom: 40px;
            width: 500px;
            border: 1px solid gray;
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-left: auto;
            margin-right: auto;
        }
    </style>
</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 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>
        const url = 'https://reqres.in/api/users';
        async function getCards() {
            let response = await fetch(url);
            cards = await response.json();
            cardsData = cards.data;
            cardsList = document.querySelector('.row');
            
            if(cardsData){
                for (key in cardsData) {
                cardsList.innerHTML += `
                <div class="col col--hidden" >
                    <div class="card">
                        <img src="${cardsData[key].avatar}" class="card-img-top"  width="200" height="200" alt="...">
                        <div class="card-body">
                            <h5 class="card-title">${cardsData[key].first_name} ${cardsData[key].last_name}</h5>
                            <p class="card-text">${cardsData[key].email}</p>
                        </div>
                    </div>
                </div>    
                `}
            } else {
                cardsList.innerHTML += `<p>Данные отсутствуют</p>`
            }
            $(".col--hidden").slice(0,3).removeClass('col--hidden');
        }

        getCards();
    </script>
    <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();
                })})
        });
        //$(document).ready(JSONToCSVConvertor(cardsData));
    </script>
</body>
</html>


server Node.js

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 createCsvWriter = require('csv-writer').createObjectCsvWriter;

//БД
const url = 'https://reqres.in/api/users';

//глобальные переменные для доступности
var cards;
var cardsData;
var cardsList;

//инициализация глобального окружения
global.document = new JSDOM(this.html).window.document;

async function getCards() {
    let response = await fetch(url);
    cards = await response.json();
    cardsData = cards.data;
    //привязка карточек к данному селектору
    cardsList = document.querySelector('.cards-list');
    console.log(cardsData);
}

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');
});

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