Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

292
Visualizações
dynamic rendering in express using loop won't work

I have bunch of static file, they are named article1, article2 and so on. So I know it worked when I do

app.get('/this-is-my-article-routes', (req, res) => {
        res.render('article1');
});

but it's too tedious and has lots of repeated code. I tried this but it doesn't work?

const articleArr = [
    'this-is-my-article-routes'
];

for (i = 0; i<9; i++) {
    app.get(`/${articleArr[i]}`, (req, res) => {
        res.render(`article${i}`);
    });
}

Is this even possible? or there's something wrong with my codes?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

how about simple solution using query params 
app.get('articleArr/:id', (req, res) => {
        res.render('article/'+req.params.id);
    });
about 4 years ago · Santiago Trujillo Relatório

0

Does your array articleArr contains 10 elements?

I think, the better way to do this would be:

var articleList = [ 'first', 'second', 'third']
app.get('/:article', (req, res) {
    var articleName = req.params.article
    var index       =  articleList.indexOf(articleName)
    if (index == -1) {
        // No Aricle Found
    }
    res.render(`articles$(index)`)
})

This code will also work fine but it is not the recommended one.

var articleList = ['first', 'second']
articleList.forEach(function(value){
    app.get('/'+value, (req, res) => {
        res.status(200).json({
            value: value
        })
    })
})
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda