Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

290
Views
la representación dinámica en express usando bucle no funcionará

Tengo un montón de archivos estáticos, se llaman artículo 1, artículo 2, etc. Así que sé que funcionó cuando lo hago

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

pero es demasiado tedioso y tiene mucho código repetido. Intenté esto pero no funciona?

 const articleArr = [ 'this-is-my-article-routes' ]; for (i = 0; i<9; i++) { app.get(`/${articleArr[i]}`, (req, res) => { res.render(`article${i}`); }); }

¿Es esto posible? o hay algo mal con mis códigos?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

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 Report

0

¿Su matriz articleArr contiene 10 elementos?

Creo que la mejor manera de hacer esto sería:

 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)`) })

Este código también funcionará bien, pero no es el recomendado.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!