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?
how about simple solution using query params app.get('articleArr/:id', (req, res) => { res.render('article/'+req.params.id); });¿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 }) }) })