Node JS write response

Node JS write response

本文关键字:response write JS Node      更新时间:2023-09-26

不知道如何给服务器写一个响应。

现在我有一个登录表单发送数据到服务器。在服务器端,我检查数据是否与。json文件中的数据具有相同的值,如果一切正确,我想发送响应。

 if (login == "login") { // if POST request comes wiht 'login' parameter
            fs.readFile("JSON/DB.json", "utf8", function (err, data) {
                
                var jsonFileArr = []; // Data from .json
                jsonFileArr = JSON.parse(data);
                
                var logPost = loginData.log; // 'log' data from request
                
                
                var gotData = jsonFileArr.find(function (obj) {
                  // Search for the same 'log' data in .json
                    return obj.log === logPost; 
                });
                
                if (gotData === undefined) { // No same log
                    console.log("ERROR: Wrong 'log' or 'pass'")
                }
                else if (gotData.log == logPost) { // if there is the same 'log' , 
                                                   // check for same 'pass'
                    
                    
                    if (gotDaten.pass == passPost) { // Same 'pass' found , send the response
                       
                        console.log("Send Response");
                        
                        
                        response.writeHead(200, { 'Content-Type': 'application/json', });
                        var resObject = { "status": "OK" };
                        var json = JSON.stringify(resObject);
                        response.end(json);
                        console.log(json);
                    }
                    else
                        console.log("ERROR: Wrong 'log' or 'pass' ");
                }
            });
        }
        else {
            console.log("Wrong request");
        }

响应没有被发送,也许我如何设置节点http服务器有问题。如何正确编写服务器响应?

当我尝试使用response.write()时,它会给我一个write after end错误。

这是整个Node JS服务器:jsfiddle

找到了问题,我只需要移动我的代码到页面被调用的地方,并使else if 循环而不是if

您可以将response.setHeader(name, value)res.send(data)一起使用

试试这个

if (login == "login") { // if POST request comes wiht 'login' parameter
  fs.readFile("JSON/DB.json", "utf8", function(err, data) {
    var jsonFileArr = []; // Data from .json
    jsonFileArr = JSON.parse(data);
    var logPost = loginData.log; // 'log' data from request

    var gotData = jsonFileArr.find(function(obj) {
      // Search for the same 'log' data in .json
      return obj.log === logPost;
    });
    if (gotData === undefined) { // No same log
      console.log("ERROR: Wrong 'log' or 'pass'")
    } else if (gotData.log == logPost) { // if there is the same 'log' , 
      // check for same 'pass'

      if (gotDaten.pass == passPost) { // Same 'pass' found , send the response
        console.log("Send Response");

        response.setHeader(
          'Content-Type', 'application/json');
          response.status(200);
        var resObject = {
          "status": "OK"
        };
        var json = JSON.stringify(resObject);
        response.send(json);
        console.log(json);
      } else
        console.log("ERROR: Wrong 'log' or 'pass' ");
    }
  });
} else {
  console.log("Wrong request");
}

如果您使用express,您可以直接使用response.json(json)