2017-04-13 89 views
0

我對node.js和web開發一般都很陌生(所以如果我完全沒有基礎,並且你有很好的材料供我使用,我真的很希望看到它)。在express上更改json node.js服務器

我試着在node.js服務器和http客戶端之間來回傳遞一個JSON對象。該客戶端到目前爲止是這樣的:

<script type="text/javascript"> 
document.getElementById("myBtn").addEventListener("click", passArg); 

function passArg() { 
    console.log("I'm here") 
    var xmlhttp = new XMLHttpRequest(); 

    xmlhttp.open("POST", "/", true); 
    xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); 

    xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == XMLHttpRequest.DONE) { 
      if (xmlhttp.status == 200) { 
       //var json = JSON.parse(xmlhttp.responseText);  
      } 
      else if (xmlhttp.status == 400) { 
       alert('There was an error 400'); 
      } 
      else { 
       alert('something else other than 200 was returned'); 
      } 
     } 
    } 

    var data = JSON.stringify({"email":"[email protected]","password":"101010"});  
    xmlhttp.send(data); 
    get_json(); 
} 

function get_json(){ 
    console.log("getting json"); 
    var xmh = new XMLHttpRequest(); 

    xmh.open("GET", "/playlist/playlist.json", true); 
    xmh.send(); 

    xmh.onreadystatechange = function() { 
     if (this.readyState == this.DONE) { 
      if (this.status == 200) { 
       var json = JSON.parse(this.responseText); 
       console.log(json.email + " "+ json.password + " " + json.access_date); 
      } 
      else if (xmh.status == 400) { 
       alert('There was an error 400'); 
      } 
      else { 
       alert('something else other than 200 was returned'); 
      } 
     } 
    } 


} 
</script> 

而且服務器端編碼爲

app.post("/", function (req, res) { 
    console.log('Request received'); 

    req.on('data', function (chunk) { 
     console.log('GOT DATA!'); 
     json = JSON.parse(chunk); 
     json.access_date = "12.04.17"; 

     write_json(json) 
     console.log("finished writing- in app_post") 
    }); 

    console.log("passing all clear to client") 
    res.writeHead(200); 

    res.send(); 

}) 

function write_json(chunk){ 
    console.log("WHADDUP") 
    console.log(JSON.stringify(chunk)) 
    fs = require("fs") 
    var filename = "./public/playlist/playlist.json"; 
    var file = require(filename); 

    file = chunk; 

    fs.writeFile(filename, JSON.stringify(file), function(err){ 
     if (err) return console.log(err); 
      console.log(JSON.stringify(file)); 
      console.log('writing to ' + fileName); 
     } 
    ) 
    console.log("finished writing - in write_json") 
    } 

上產生

Request received 
passing all clear to client 
GOT DATA! 
WHADDUP 
{"email":"[email protected]","password":"101010","access_date":"12.04.17"} 
module.js:428 
    throw err; 
    ^

SyntaxError: public/playlist/playlist.json: Unexpected end of input 

而在客戶端會產生如下輸出服務器端控制檯控制檯讀取的東西

(index):15 I'm here 
(index):41 getting json 
(index):45 GET http://localhost:8080/playlist/playlist.json net::ERR_CONNECTION_RESET 

從此我讀了異步POST事件正在發送全部清除以在文件本身更新之前調用get_json。而且似乎在服務器端的文件更新不起作用。如何構建調用以使編輯非常流暢?

回答

0

快速回答是創建一個函數來調用res.send,將它傳遞給您的write_json函數,並從writeFile回調中調用它。

app.post("/", function (req, res) { 
    console.log('Request received'); 

    req.on('data', function (chunk) { 
     ... 

     write_json(json, function() { 
      console.log("passing all clear to client") 
      res.writeHead(200); 

      res.send(); 
      console.log("finished writing") 
     });    
    });  
}); 

function write_json(chunk, onFinishedWriting){ 
    ... 
    fs.writeFile(filename, JSON.stringify(file), function(err){ 
     if (err) { 
      return console.log(err);     
     } 
     else { 
      console.log(JSON.stringify(file)); 
      console.log('writing to ' + fileName); 
      onFinishedWriting(); 
     } 
    }); 

} 

但是,當你發現自己寫這樣的代碼:

  }); 
    }); 
}); 

那麼你很可能在衆所周知的「回調地獄」。我懷疑你想要的是使用承諾。 Here's a question specifically about promises and writeFile這可能會幫助你。

相關問題