简体   繁体   中英

NODE.JS with express having many routes

I am trying to create a website that has many routes and don't want to put all my route/ pages into app.js, I would like to put them organize in folders

I am trying to create a site like (( https://www.coca-colacompany.com/brands/smartwater ))

Where it has a folder like ( brands ) and in that specific folder goes into ( smartwater )

Basically what I am trying to achieve using Node.js is for my URL Link to look like the coca-cola website. ..... com/ brands / smartwater

I hope explain as much, thank you in advance

As stated by other coments next.js would do it all for free and allow you to use react for frontend. best option IMHO.

If you prefer to do it manually with express, the answer is here: How to include route handlers in multiple files in Express? take time to search in stackoverflow before reposting the same question.

If you want to put the routes in a separate file, for example routes.js, you can create the routes.js file in this way:

// routes/brand.js
module.exports = function(app){
    app.get('/brand/test', function(req, res){
        res.render('brand_test', {
            title: 'hello'
        });
    });

    //other routes..
}

And then you can require it from app.js passing the app object in this way:

require('./routes/brand')(app);

there are several other methods you can use. please read https://expressjs.com/fr/guide/routing.html if you want to see more.

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