简体   繁体   English

如何处理 Node.js 上的动态页面

[英]How to handle the dynamic page on Node.js

I am using the Node.js(Express.js) and EJS.我正在使用 Node.js(Express.js) 和 EJS。

My query is how I can manage the Dynamic page.我的查询是如何管理动态页面。 Here Dynamic means admin create the page, that page will be accessible and render according to the name of the category.这里动态意味着管理员创建页面,该页面将根据类别的名称进行访问和呈现。

Statically I can handle with create the file.ejs on view folder, then on server file I can write the code like:静态我可以处理在视图文件夹上创建 file.ejs,然后在服务器文件上我可以编写如下代码:

app.get('/testpage', (req, res) => {
 res.render('/test.ejs')
})

So on type the url page will serve the content which are written under test.ejs file.因此,键入 url 页面将提供在 test.ejs 文件下编写的内容。

But I want to handle all this things Dynamically.但我想动态地处理所有这些事情。

I have done this type of things through Jquery where I created the page and then call the data through api (ajax), then render the element under the target content.我通过创建页面的 Jquery 完成了此类事情,然后通过 api (ajax) 调用数据,然后在目标内容下渲染元素。

Is there any one who can suggest this through a short and easiest manner which will be efficient and best programmig approach.有没有人可以通过一种简短而简单的方式提出这一建议,这将是一种有效且最佳的编程方法。

See routing in the Express.js guide.请参阅 Express.js 指南中的路由

Pull the selected category out of the URL with params, then look it up in the database, and pass the resulting data to your EJS template.使用参数将所选类别从 URL 中拉出,然后在数据库中查找,并将结果数据传递给您的 EJS 模板。

You can create a route such as:您可以创建一个路由,例如:

app.get('/users/:category', function (req, res) {
    const category = req.params.category;
    getDataAboutCategoryFromDatabase(category).then(
        data => red.render("category.ejs", data)
    );
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM