Search code examples
javascriptnode.jsexpressconfigurationconfiguration-files

NodeJS Express - How to Create Configuration File for POST Request?


I using Express in NodeJS to accept URL POST Request Call then the system will response a JSON File upon Request. My current implementation is in a Hardcode method, as whenever I want to add more POST Request Input and define which JSON File to be read I will need to manually add on more code such as if-else statement in the Javascript File.

My Desire Output:

Is it possible to make create a Configuration File with JSON or YAML where allow user to edit the Configuration File whenever they wanted to add in more Post Request Input and add in more condition for which JSON File to be open instead of manually add on more code to the Javascript File?

My motive is to allow user to have a easy modification without touching the implementation code in the Javascript File itself but they just have to deal with the Configuration File instead.

Hardcode Method: (UPDATED)

const postConfig = require( "./config/postConfig" )

// before allow configuration, the code is
// app.post('/api',function (req, res, next)
// after configuration, I just need to change the value in postConfig.json to determine the POST URL.
app.post(postConfig.URL,function (req, res, next) {

    // HARDCODE AREA - The POST Request Input
    // What I want is make this 'userID' is configurable/changable using JSON file. 
    // Which mean its value will based on the value at JSON file. But not HARDCODE it as 'userID'. 
    // If I want to change to 'foodID', I just change it at JSON file.
    var input = req.body.userID,


  // HARDCODE AREA - ALL OF THE IF ELSE Statement
  if(input =="1"){

    var contents = fs.readFileSync(publicdir+"/api/a.json");
    var jsonContent = JSON.parse(contents);
    res.send(jsonContent);

  }else if(input =="2"){

    var contents = fs.readFileSync(publicdir+"/api/b.json");
    var jsonContent = JSON.parse(contents);
    res.send(jsonContent);

  }else{
    res.status(404);
    res.json({
        error: {
            code: 404  + " - Invalid ID"
        }
    });
  }


});

postConfig.json:

// This allow configuration for the POST URL
{
  // I can change to 
  // "URL" : "/api" or any value i want
  "URL" : "/plus" 
}

Solution

  • You can create a map for example:

    var testMap = {
       1 : "a",
       2 : "b",
       3 : "c"
    }
    

    and use this map(can also be used as a separate JSON file) instead of if else statements

    var checkJSONFile = testMap[user.userID];
    if(checkJSONFile){
      var contents = fs.readFileSync(publicdir+"/api/"+checkJSONFile+".json");
      var jsonContent = JSON.parse(contents);
      res.send(jsonContent);
    }else{
      res.status(404);
      res.json({
        error: {
          code: 404  + " - Invalid ID"
        }
      });
    }
    

    Update:

    var userId = req.body.userID;
    req.body[<key_from_json>] = userId;
    // Rest of the code follows