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

197
Views
creando rutas dinámicas usando nodejs

aplicación.js

 // Calling Routes require("./routes")(app);

carpeta del enrutador index.js

 module.exports = function (app) { app.use("/", require("./all_routes")); }

todas_rutas.js

 var express = require("express"); var router = express.Router(); router.get("/", function (req, res, next) { res.render("home/index.html"); }); //About Page router.get("/about", function (req, res, next) { res.render("about/index.html"); }); //Contact router.get("/contact", function (req, res, next) { res.render("contact/index.html"); }); //product router.get("/product", function (req, res, next) { res.render("product/index.html"); }); //product list router.get("/product/demo-product", function (req, res, next) { res.render("demo-product/index.html"); }); router.get("/product/request-product", function (req, res, next) { res.render("request-product/index.html"); }); //service router.get("/service", function (req, res, next) { res.render("product/index.html"); }); //service list router.get("/service/what-we-do", function (req, res, next) { res.render("what-we-do/index.html"); }); router.get("/service/how-we-do", function (req, res, next) { res.render("how-we-do/index.html"); });

Estoy tratando de reducir el código en el archivo all_routes.js , el mismo código se repite una y otra vez.

Busqué en línea e intenté crearlo dinámicamente, pero no tuve éxito. ¿Hay alguna forma en que pueda reducir la línea de código como he dado el seguimiento de mi código anterior?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Si desea reducir el estándar de todas sus rutas de get , una opción es crear un objeto para asignar sus rutas a los archivos que están cargando. Luego puede iterar sobre ese objeto y agregar las rutas a su enrutador.

 const routes = { "/": "home/index.html", "/about": "about/index.html", "/contact": "contact/index.html" // Add others here } for (let key in routes) { router.get(key, function (req, res, next) { res.render(routes[key]); }); }

Editar: si sus rutas son consistentes en que el archivo index.html siempre estará en el directorio con el nombre de la parte después de la última / en su ruta, puede usar una matriz y alguna lógica elegante. ¡Simplemente no rompas la regla!

 const routes = [ "/contact", "/product", "/product/demo-product", "/product/request-product" ] routes.forEach(route => { const path = /[^/]*$/.exec(route)[0]; router.get(route, function (req, res, next) { res.render(`${path}/index.html`); }); })
about 4 years ago · Juan Pablo Isaza 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!