2
\$\begingroup\$

I am going to have multiple APIs in routes folder should I put them in in the same file API or separate them?

├── app.js
├── src/
│   ├── contants/
│   ├── helpers/
│   ├── models/
│   ├── routes/
|   |         |___index.js
              |___api.js
│   └── libs/
│       ├── backbone/
│       ├── underscore/
│       └── ...
api.js file contains all the APIs
    const jwt = require("jsonwebtoken")
    const axios = require("axios")
    require("express-async-errors")
    const bodyParser = require("body-parser")
    const fs = require("fs")

    const LOLTrackingSystem = require("../methods/onlineGamesTracking/LOLTracking")
    const getUserData = require("../methods/leagueOfLegends/getUserData")
    const isAuthenticated = require("../helpers/authenticated")

    const apiRoute = (api) => {
      api.use(bodyParser.json())
      api.use(bodyParser.urlencoded({
        extended: false
      }));

      api.post("/api/auth", (req, res) => {
        //API Functions
      })

      api.post("/api/gizmo/memberProfile", isAuthenticated, (req, res) => {
        //API Functions
      })

      api.post("/api/gizmo/memberState/:userId/:host/:state", async (req, res) => {
        //API Functions
      })
    }

    module.exports = apiRoute

Is what I am doing is right?

If it's wrong what is the right way to do it?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Please edit your question so that the title describes the purpose of the code, rather than its mechanism. We really need to understand the motivational context to give good reviews. Thanks! \$\endgroup\$ Commented Feb 12, 2020 at 9:56

1 Answer 1

6
\$\begingroup\$

there is no right in wrong in this situation it's a question of what's best for your particular situation if you are going to build many Rest API endpoints it's best to separate them in separate files under routes like this so your code can be more maintainable :

│   ├── routes/
|   |         |___index.js
              |___auth.js
              |___gizmo.js
                          - 
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.