0

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?

mohamed adel
  • 695
  • 1
  • 15
  • 36
  • 1
    try this https://stackoverflow.com/questions/6059246/how-to-include-route-handlers-in-multiple-files-in-express – Uthistran Selvaraj Jan 07 '20 at 20:13
  • I know how to export the API functions and split them in files. My question is what is the best practices? leave all the API routes in the API files or seperate them for example auth.js memberProfile.js memberState.js – mohamed adel Jan 07 '20 at 20:17
  • 1
    I prefer separating the apis so it will help us the main api.js untouched all new addition. I used NestJs in my application, Also try using decorators – Uthistran Selvaraj Jan 07 '20 at 20:21
  • 1
    There's nothing special about routes in this regard. Imagine you have 20 functions in your app that are used in various places. Do you put them all in one module and export them all from there? Or, do you break them into 20 separate files? Or do you group them into more than 1 and less than 20 files? The answer depends entirely upon how related they are to one another, what matters to the code that uses them, what code might ever be turned into a shared module for another app, etc... It's just standard choices about modularity. Use that same logic for routes. There is no "right" answer. – jfriend00 Jan 07 '20 at 20:28
  • 1
    I personally dislike seeing one function per file. I just think it's inconvenient for working in the project and makes things seem more complicated than they need to be. I'd like to see a bunch of related routes bunched together in one file. How you interpret "related" is up to you. – jfriend00 Jan 07 '20 at 20:30

1 Answers1

1

It really depends on your personal preferences.

If you prefer having all of the functions in one file, that's okay. One good thing about doing this is that you don t have to keep track of requiring other files.

What I think you should think about is how related the API'S are to each other, how many different functions you have, and the length of the functions. If you don't have many functions then you should keep them all in one file. However if you have many big, independent api's then it can be more organized keeping them in separate files.

In the end, there is not right or wrong answer. You decide based on your style, and if you wanted advice and opinions there are plenty in the comments. Good luck.

programmerRaj
  • 1,810
  • 2
  • 9
  • 19