H
H
hollanditkzn2017-09-15 13:08:44
JavaScript
hollanditkzn, 2017-09-15 13:08:44

How to output everything to the server after the parser?

After the page parser, it outputs in this format
Heading1
Heading2
Heading3
...
HeadingN
Only I did not understand how to output these values ​​to the server
My code

request('https://nix-tips.ru/tag/yii2', function (err, respons, html) {
    if (!err && respons.statusCode === 200){
        let $ = cheerio.load(html);
        $('article').each(function(i, element){
            let title = $(this).find('.entry-title');
            let date = $(this).find('.entry-date');
            let content = $(this).find('p');
            let arr = [];
            while (date < 50){
                arr.add(date);
            }
            app.get('/', function(req, res){
                res.render('main', {
                    header: 'Новости по yii2',
                    date: $(date).text(),
                    title: $(title).text(),
                    content: $(content).text(),
                });
            });
        });
    } else {
        console.log('Произошла ошибка!');
    }
});

And template
<div class="entry">
    <h1>{{header}}</h1>
    {{#if error}}
        <h2>Ошибка!{{error}}</h2>
    {{/if}}
    <div class="body">
        <ul class="people_list">
            <form action="">
                <select name="col" id="news">
                    <option value="5">5</option>
                    <option value="10">10</option>
                    <option value="15">15</option>
                    <option value="20">20</option>
                </select>
            </form>
            <div class="content">
                {{#each date}}
                <h2>{{this}}</h2>
                {{/each}}
                {{#each title}}
                <p>{{this}}</p>
                {{/each}}
                {{#each content}}
                <p>{{this}}</p>
                {{/each}}
            </div>
        </ul>
    </div>
</div>

How to have a value of at least [Heading1, Heading2, Heading3, ..., HeadingN] so that you can iterate over the elements

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Gerasimov, 2017-09-15
@hollanditkzn

Something like this:

app.get('/', (req, res) => {
  request('https://nix-tips.ru/tag/yii2', (err, response, html) => {
    if (err || respons.statusCode !== 200) return console.log("AAAAAAAAAA", err);

    let $ = cheerio.load(html);
    let results = [];		

    $('article').each(function(i, element) {
      results.push({
        title : $(this).find('.entry-title'),
        date : $(this).find('.entry-date'),
        content : $(this).find('p')
      })
    });

    return res.render('main', {
      header : 'Новости по yii2',
      results
    })
  });
});

ps. I slightly rewrote the order, I hope you will think why I did this (this is important)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question