Javascript 将数据发送到nodejs服务器

Javascript 将数据发送到nodejs服务器,javascript,node.js,xmlhttprequest,Javascript,Node.js,Xmlhttprequest,我现在正在学习nodejs,我有几件事要做。 我正在创建一个服务于html文件的服务器。该html文件有一个执行xmlHttpRequest以获取数据的js。我要将正在检索的数据发送回服务器进行处理。最后一步就是我被困的地方。每次服务器停止时,我都希望在服务器中接收URL来处理它们 Server.js var http = require('http'), url = require('url'), path = require('path'), fs = require

我现在正在学习nodejs,我有几件事要做。 我正在创建一个服务于html文件的服务器。该html文件有一个执行xmlHttpRequest以获取数据的js。我要将正在检索的数据发送回服务器进行处理。最后一步就是我被困的地方。每次服务器停止时,我都希望在服务器中接收URL来处理它们

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);
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);
}
Client.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);
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);
}
函数getURL(){ var moduleURL=document.getElementById(“url”).value; var url=[]; 控制台日志(moduleURL); xmlhttp=新的XMLHttpRequest(); xmlhttp.onreadystatechange=函数(){ if(xmlhttp.readyState==4&&xmlhttp.status==200){ var xml=xmlhttp.responseXML; var items=xml.children[0]。children[0]。children; 对于(var i=13;i events.js:72 thrower;//未处理的“error”事件^error:EISDIR,读取

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

您需要做的是确保该文件确实是一个文件,因为函数fs.exists()仅用于文件

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

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);

看起来你的服务器没有一个能捕获ajax URL的路由,不管是什么?你能再解释一下吗?当你向URL发送POST数据时,服务器必须捕获它。你将它发送到
/
,你有一个能捕获所有内容的服务器,但它似乎什么也不做,也不返回任何内容,可能是什么We’就像这样->@adeneo你能把它作为一个答案发布出来,这样我就可以接受吗?因为你的代码片段真的帮了我的忙。你能帮我把数据发送回我的客户吗(我问题的第二部分)?当我处理URL时,我正在进行计算,以显示已处理的百分比,并且它正在工作,我只想将其输出到html中。这有点复杂,使用ajax时,您实际上只能返回一次数据,而不能在请求进行时返回。您可能需要进行某种长时间的轮询,以恢复百分比结束它,或者使用websockets,在ajax请求发送数据时,您可以使用套接字将百分比发送回浏览器。我也实现了您的片段,但我将资源解释为样式表,但使用MIME类型text/plain进行传输:“.js也是一样。我做错了什么?因为从我的角度看,这似乎很好??