简体   繁体   中英

How to handle the dynamic page on Node.js

I am using the Node.js(Express.js) and 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:

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.

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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