0

app.js

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

router folder index.js

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

all_routes.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");
});

I am trying to reduce the code in all_routes.js file has same code is repeating again and again

I searched online and trying to create it dynamically but getting no success is there any way I can reduce the line of code as I have given the follow of my code above

1
  • If you're not actually using template features and just need the appropriate index.html file sent to the browser, you appear to have enough correlation between URL and file system location here that you could do this with just a couple express.static() routes or you could write a slightly different version of express.static() to fetch the matching file from the file system. Commented Nov 26, 2021 at 4:24

1 Answer 1

1

If you'd like to cut down on boilerplate of all your get routes, one option is to create an object to map your routes to the files they're loading. Then you can iterate over that object and add the routes to your router.

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]);
  });
}

Edit: If your routes are consistent in that the index.html file will always be in the directory named after the part after the last / in your route, you can potentially use an array and some fancy logic. Just don't break the rule!

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`);
  });
})
Sign up to request clarification or add additional context in comments.

6 Comments

if there are 20+ routes then I have to add every routes and path in const routes manually
Sure! I guess you could potentially find some shortcuts if there's a particular way you name your routes to map them to files in your file system. Other than telling the program what routes should map to what files, how were you imagining your program would know that information?
ya I got it but I thought if the url and file and path name are same so i can use both of them but lemme try this
Yeah, if they're the same you could just do an array and loop over that array, but from what I can tell there are some nuances in your routes.
@Vikas I updated my answer to give you a fancier option if your routes all have a consistent file path convention. Notably you'll have to do any routes that don't match the pattern (e.g., the home route) separately
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.