在response.write()中TypeError:第一个参数必须是string或Buffer

时间:2016-06-13 10:18:41

标签: node.js

我在Node中创建了一个服务器,我通过它调用fb.html。现在,当我访问http://localhost:8081/时,我继续收到以下错误: http://oi67.tinypic.com/rkde9d.jpg

// server.js

var http = require("http");
var fs = require("fs");
http.createServer(function (request, response) {

   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
fs.readFile("fb.html","utf8", function(err, data){
  response.writeHead(200, {'Content-Type': 'text/html'});
  response.write(data.toString());
  response.end();
});

}).listen(8081);

// Console will print the message
console.log('Server running at http://127.0.0.1:8081/');

// fb.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title></title>
  <link rel="stylesheet" href="">
</head>
<body>

  <script>

  var acces_token, object_id=null,id;
    // initialize and setup facebook js sdk
    window.fbAsyncInit = function() {
        FB.init({
          appId      : 'myappID',
          xfbml      : true,
          version    : 'v2.6'
        });

    };

    (function(d, s, id){
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) {return;}
        js = d.createElement(s); js.id = id;
        js.src = "https://connect.facebook.net/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));

// function to login to facebook
function myFBLogin() {
FB.getLoginStatus(function(response) {

  if (response.status === 'connected') {
  alert("You are already logged in!");
  }
  else{
  FB.login(function(response){
  // user is now logged in
    console.log(response);
    acces_token=response.authResponse.accessToken;
  }, {scope: 'publish_actions' , scope:'user_posts' });

  }
  });
  }

// function to logout
function myFBLogout(){

FB.getLoginStatus(function(response) {

  if (response.status === 'unknown') {
  alert("You are already logged out!");
  }
  else{
  console.log(response);
  FB.logout(function(response) {
  // user is now logged out
  alert("You are successfully Logged Out!");
  acces_token=null;
  if(object_id==null)
  document.getElementById('status').innerHTML="Post ID is unknown";
  else 
  document.getElementById('status').innerHTML="You are logged out";
});
 }
 });

}

// function to post an image on wall
function myFacebookPost()
{
  FB.api("/me/photos",

    'post',

      {url: document.getElementById("frm1").elements[0].value},
    function (response){
      console.log(response);
      if(response.error){
      if(response.error.code==2500)
      {
        alert("You are not logged in!");
      }
      else if(response.error.code==1)
      {
        alert("Image url is not correct!");
      }
      else if(document.getElementById("frm1").elements[0].value=="") alert("Enter the URL!");
    }
    else{
      id=response.post_id;
      object_id=response.id;
      document.getElementById('status').innerHTML=0;
    }
    }
    );
}

//function to count number of likes
function like_count(){

  var a=FB.getAuthResponse();   

  FB.api(
    "/"+object_id+"/likes", 
    'get',
    function (response) {
    console.log(response);
      if(response.error){
     if(object_id==null)
      {
        alert("Object ID is not defined. First create a post!");
      }
      else if(response.error) 
      {
        alert("You are not logged in!");
      }
    }
    else 
      document.getElementById('status').innerHTML=response.data.length;

    }
);
}


  </script>
  <button onclick="myFBLogin()">Login to Facebook</button>
  <button onclick="myFBLogout()">Logout</button><br><br>

  <form id="frm1" action="javascript:myFacebookPost()">
    Image URL: <input type="text" name="URL" placeholder="Enter the image url" >
    <input type="submit" value="Post Image on Wall" style="display: inline">
  </form>
<br>

  <button onclick="like_count()"  >Likes count</button>

  <p style="display: inline">  &ensp;&ensp;Likes Count = &ensp;</p>
  <div id="status" style="display: inline"> Post ID is unknown</div>

</body>
</html>

请帮助找到问题所在。好吧,我有这个,如果在系统上工作正常,而在其他人上它会引起上述错误。

0 个答案:

没有答案