Y
Y
Yan Vetrov2019-03-21 15:37:06
Node.js
Yan Vetrov, 2019-03-21 15:37:06

Is it possible to use more than one app.render()?

There is a question how to do the same task of rendering news without using a lot of app.render(). Is it possible to use only one app.render()?

The code:

const express = require('express')
const app = express();
const hbs = require('handlebars')
const cons = require('consolidate');
const bodyParser = require('body-parser')
const urlencodedParser = bodyParser.urlencoded({ extended: false })

var yandex = {
    newsOne: 'Новость 1',
    newsTwo: 'Новость 2',
    newsThree: 'Новость 3',
    newsFour: 'Новость 4',
    newsFive: 'Новость 5'
}

var lenta = {
    newsOne: 'Новость 1',
    newsTwo: 'Новость 2',
    newsThree: 'Новость 3',
    newsFour: 'Новость 4',
    newsFive: 'Новость 5'
}

var tj = {
    newsOne: 'Новость 1',
    newsTwo: 'Новость 2',
    newsThree: 'Новость 3',
    newsFour: 'Новость 4',
    newsFive: 'Новость 5'
}

app.engine('hbs', cons.handlebars)
app.set('view engine', 'hbs')


app.get('/', function(req, res) {
    res.render('index', {})
})

app.post('/', urlencodedParser, function(req, res) {
    if (req.body.news == 'yandex') {
        console.log('Был получен запрос: Яндекс.Новости')
        res.render('index', yandex)
    } else if (req.body.news == 'lenta') {
        console.log('Был получен запрос: Лента')
        res.render('index', lenta)
    } else if (req.body.news == 'tj') {
        console.log('Был получен запрос: TJ')
        res.render('index', tj)
    }
})

app.listen('9000')

And there is an hbs template:
1. This is responsible for issuing news
<div id="getNews">
            <p>
                <p>{{newsOne}}</p>
                <p>{{newsTwo}}</p>
                <p>{{newsThree}}</p>
                <p>{{newsFour}}</p>
                <p>{{newsFive}}</p>
            </p>
        </div>

2. This is for news selection:
<form id="input" method="post">
            <p>Пожалуйста, выберите источник, с которого мы будем отправлять Вам новости:</p>
            <div>
                <input type="radio" id="yandex"
                name="news" value="yandex" form="input">
                <label for="yandex">Яндекс Новости</label>

                <input type="radio" id="lenta"
                name="news" value="lenta" form="input">
                <label for="lenta">Лента</label>

                <input type="radio" id="tj"
                name="news" value="tj" form="input">
                <label for="tj">TJ</label>
            </div>
            <div>
                <button type="submit">Submit</button>
            </div>
        </form>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Zhivagin, 2019-03-21
@id_pripyat

Which app.render() are you talking about exactly?
If I understand correctly, you want to remove code duplication in the function that listens to POST to the url '/' ? Then like this, for example:

var news = {
    yandex: {
        newsOne: 'Новость 1',
        newsTwo: 'Новость 2',
        newsThree: 'Новость 3',
        newsFour: 'Новость 4',
        newsFive: 'Новость 5'
    },
    lenta: {
        newsOne: 'Новость 1',
        newsTwo: 'Новость 2',
        newsThree: 'Новость 3',
        newsFour: 'Новость 4',
        newsFive: 'Новость 5'
    },
    tj: {
        newsOne: 'Новость 1',
        newsTwo: 'Новость 2',
        newsThree: 'Новость 3',
        newsFour: 'Новость 4',
        newsFive: 'Новость 5'
    },
}

...

app.post('/', urlencodedParser, function(req, res) {
    console.log('Был получен запрос: ' + req.body.news)
    res.render('index', news[req.body.news]);
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question