简体   繁体   English

NodeJS Express-如何为POST请求创建配置文件?

[英]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. 我在NodeJS中使用Express接受URL POST Request Call然后系统将根据请求响应JSON File 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 . 我当前的实现是在Hardcode方法中进行的,因为每当我想添加更多的POST Request Input并定义要读取的JSON File ,都需要手动添加更多code例如Javascript File中的if-else语句。

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 ? 是否可以使用JSON或YAML创建配置文件,从而使用户可以在想要添加更多的Post Request Input并添加更多条件以打开JSON File而不是手动添加更多代码的情况下,随时编辑配置文件到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. 我的动机是允许用户轻松进行修改,而无需触碰Javascript File本身中的实现代码,但他们只需要处理配置文件即可。

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: postConfig.json:

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

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 并使用此地图(也可以用作单独的JSON文件)代替if else语句

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM