2015-03-18 344 views
0

我正在学习nodejs,并且遇到了一些问题。 我正在创建一个提供html文件的服务器。该HTML文件有一个js,它执行xmlHttpRequest来获取数据。我要检索的数据要发送回我的服务器进行处理。最后一步是我卡住的地方。每次服务器停止时,我都希望收到服务器中的URL来处理它们。将数据发送到nodejs服务器

Server.js

var http = require('http'), 
    url = require('url'), 
    path = require('path'), 
    fs = require('fs'); 

var mimeTypes = { 
    "html": "text/html", 
    "js": "text/javascript", 
    "css": "text/css"}; 

    http.createServer(function(request, response){ 

     var uri = url.parse(request.url).pathname; 
     var filename = path.join(process.cwd(), uri); 

     fs.exists(filename, function(exists){ 
      if(!exists){ 
       console.log(filename + " does not exist"); 
       response.writeHead(200, {'Content-Type' : 'text/plain'}); 
       response.write('404 Not found\n'); 
       response.end(); 
       return; 
      } 

      var mimeType = mimeTypes[path.extname().split(".")[1]]; 
      response.writeHead(200, {'Content-Type' : mimeType}); 

      var fileStream = fs.createReadStream(filename); 
      fileStream.pipe(response); 
     }); 

    response.on('end', function(){ 
     console.log("Request: " + request); 
     console.log("Response: " + response); 
    }); 

    }).listen(1337); 

Client.js

function getURLs(){ 
    var moduleURL = document.getElementById("url").value; 
    var urls = []; 
    console.log(moduleURL); 

    xmlhttp = new XMLHttpRequest(); 
    xmlhttp.onreadystatechange=function(){ 
     if (xmlhttp.readyState==4 && xmlhttp.status==200){ 
      var xml = xmlhttp.responseXML; 
      var items = xml.children[0].children[0].children; 

      for(var i = 13; i<items.length; i++){ 
       urls.push(items[i].children[1].getAttribute("url")+"&hd=yes"); 
      } 

      //console.log(urls); 
      sendDataToServer(urls); 
     } 
     } 
    xmlhttp.open("GET", moduleURL, true); 
    xmlhttp.send(); 

} 

function sendDataToServer(urls){ 
    //console.log(urls); 

    var http = new XMLHttpRequest(); 
    http.open("POST", "http://127.0.0.1:1337/", true); 
    http.send(urls); 
} 

我在浏览器

POST http://127.0.0.1:1337/ net::ERR_CONNECTION_REFUSED

的控制台让这个和这个在cmd中节点

events.js:72 throw er; // Unhandled 'error' event ^Error: EISDIR, read

虽然它正在处理数据,但我想将进度发送回客户端以显示在最终用户的html页面上。我已经有了进展的功能,它只是发送/接收我卡住的数据。请有人指出我正确的方向吗?

我也知道,有快递和其他模块,我可以使用,但学习节点我试图这样做。所以我希望有人能把我推向正确的方向。

+0

它看起来不像你的服务器有一条路线,可以捕获ajax URL,不管是什么? – adeneo 2015-03-18 22:02:44

+0

你能多解释一下吗? – mXX 2015-03-18 22:09:42

+1

当您将POST数据发送到URL时,服务器必须捕获它。你将它发送给'/',并且你有一个捕获所有东西的服务器,但它似乎没有做任何事情或返回任何东西,也许更像这样 - > ** http://jsfiddle.net/5eeLn17o/** – adeneo 2015-03-18 22:15:21

回答

0

events.js:72 throw er; // Unhandled 'error' event^Error: EISDIR, read

此错误表示您尝试读取的文件实际上是一个目录。

你需要做的是确保文件是一个真正的文件作为函数fs.exists()只适用于文件。

在本例中,fs.lstat()用于获取一个fs.stat对象,该对象具有您需要确保文件类型正确的方法。

var http = require('http'), 
url = require('url'), 
path = require('path'), 
fs = require('fs'); 

var mimeTypes = { 
    "html": "text/html", 
    "js": "text/javascript", 
    "css": "text/css" 
}; 

http.createServer(function(request, response){ 

    var uri = url.parse(request.url).pathname; 
    var filename = path.resolve(path.join(process.cwd(), uri)); 
    console.log(filename); 

    // Get some information about the file 
    fs.lstat(filename, function(err, stats) { 

     // Handle errors 
     if(err) { 
     response.writeHead(500, {'Content-Type' : 'text/plain'}); 
     response.write('Error while trying to get information about file\n'); 
     response.end(); 
     return false; 
     } 

     // Check if the file is a file. 
     if (stats.isFile()) { 

     fs.exists(filename, function(exists){ 
      if(!exists){ 
       console.log(filename + " does not exist"); 
       response.writeHead(200, {'Content-Type' : 'text/plain'}); 
       response.write('404 Not found\n'); 
       response.end(); 
       return; 
      } 

      var mimeType = mimeTypes[path.extname().split(".")[1]]; 
      response.writeHead(200, {'Content-Type' : mimeType}); 

      var fileStream = fs.createReadStream(filename); 
      fileStream.pipe(response); 
     }); 

     } else { 
     // Tell the user what is going on. 
     response.writeHead(404, {'Content-Type' : 'text/plain'}); 
     response.write('Request url doesn\'t correspond to a file. \n'); 
     response.end(); 
     } 

    }); 

response.on('end', function(){ 
    console.log("Request: " + request); 
    console.log("Response: " + response); 
}); 

}).listen(1337); 
+0

我也实现了你的代码段,但是我将Resource解释为Stylesheet,但是使用MIME类型text/plain传输:“http://127.0.0.1:1337/css/style.css”。和js一样。我究竟做错了什么?因为我怎么看它,似乎很好? – mXX 2015-03-20 23:11:38