正在将数据发送到nodejs服务器

Sending data to nodejs server

本文关键字:nodejs 服务器 数据      更新时间:2023-09-26

我现在正在学习nodejs,我遇到了一些问题。我正在创建一个提供html文件的服务器。该html文件有一个js,它执行xmlHttpRequest来获取数据。我正在检索的数据我想发送回我的服务器进行处理。最后一步是我陷入困境的地方。每次服务器停止时,我都希望接收服务器中的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);

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

我在浏览器的控制台上得到了这个

岗位http://127.0.0.1:1337/net::ERR_CONNECTION_REFUSED

在节点的cmd中

events.js:72投掷者;//未处理的"错误"事件^错误:EISDIR,读取

在处理数据的同时,我想将进度发送回客户端,以便在html页面上显示给最终用户。我已经有了进度的功能——这只是我被卡住的数据的发送/接收。有人能给我指正确的方向吗?

我也知道我可以使用express和其他模块,但为了学习node,我正在尝试这样做。所以我希望有人能把我推向正确的方向。

events.js:72投掷;//未处理的"错误"事件^错误:EISDIR,读取

这个错误意味着你试图读取的文件实际上是一个目录。

您需要做的是确保该文件确实是一个文件,因为函数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);