-1

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

international
  • 21
  • 1
  • 1
  • 3
  • 1
    I would look into Next.JS. The folder/file structure you describe is how it handles the routing. – Will Walsh Jun 02 '21 at 22:01
  • 1
    What is your question? SO is not a code-writing service. We help debugging your existing code. Please share what you got so far. – Dominik Jun 02 '21 at 22:07

1 Answers1

1

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.

Raphael PICCOLO
  • 2,095
  • 1
  • 12
  • 18