使用 nodejs 创建动态路由
creating dynamic routes using nodejs
app.js
// Calling Routes
require("./routes")(app);
路由器文件夹
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");
});
我正在尝试减少 all_routes.js
文件中的代码有相同的代码一次又一次地重复
我在网上搜索并尝试动态创建它,但没有成功,有什么方法可以减少代码行,因为我已经给出了上面的代码
如果您想减少所有 get
路由的样板文件,一种选择是创建一个对象以将您的路由映射到它们正在加载的文件。然后您可以遍历该对象并将路由添加到您的路由器。
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]);
});
}
编辑: 如果您的路由一致,index.html
文件将始终位于以最后一个 /
之后的部分命名的目录中你的路线,你可以潜在地使用一个数组和一些奇特的逻辑。只是不要违反规则!
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`);
});
})
app.js
// Calling Routes
require("./routes")(app);
路由器文件夹 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");
});
我正在尝试减少 all_routes.js
文件中的代码有相同的代码一次又一次地重复
我在网上搜索并尝试动态创建它,但没有成功,有什么方法可以减少代码行,因为我已经给出了上面的代码
如果您想减少所有 get
路由的样板文件,一种选择是创建一个对象以将您的路由映射到它们正在加载的文件。然后您可以遍历该对象并将路由添加到您的路由器。
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]);
});
}
编辑: 如果您的路由一致,index.html
文件将始终位于以最后一个 /
之后的部分命名的目录中你的路线,你可以潜在地使用一个数组和一些奇特的逻辑。只是不要违反规则!
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`);
});
})