2012-06-07 2 views
0

Я работал на сервере node.js для проекта. В настоящее время у меня есть скрипт на html-странице, который передает данные POSTS на страницу jsp на том же сервере. Однако я не могу отправить данные обратно по трубе. Я отметил то же самое с различными сценариями публикации node.js.Ответ на сообщение с node.js не работает

EDIT: Где я говорю JSP, я имею в виду стандартный javascript; Я просто сделал страницы с расширениями .jsp вместо .js, поэтому я все равно мог отправлять скрипты как .js.

EDIT 2: Заголовки/do/получают отправленные. Просто кажется, что response.write() или reponse.end() не передают ничего. Я пробовал gzipping данные и изменяю заголовок передачи-кодировки, чтобы соответствовать без везения. Думаю, я смогу провести это.

EDIT 3:. Wireshark не может обнаружить сообщения по какой-то причине>>

EDIT 4: Замечено, что response.write() возвращает ложь. Не уверен, как отлаживать. Я добавлю еще немного кода в конце.

Код JSP-страницы.

/** 
* This POST parser takes XML data and translates it as follows.. 
* 
* XML -> JS Object -> XML 
* 
* This is so I can test some libraries. 
*/ 

// Eyes, to inspect variables 
var inspect = require('eyes').inspector({maxLength: false}); 

// This is an XML parser, that parses XML into standard JS arrays 
var xml2js = require('xml2js'); 

// This one converts JSON (Or JS objects) to XML. 
var jsontoxml = require('jsontoxml'); 

exports.seperateHeader = false; // We have no separate header function 
exports.separateEnd = true; 

exports.GET = function(query, request, response) { // Our GET function. 

    response.writeHead(200, { // Write the header 
     'Content-Type': 'text/plain; charset=utf-8' 
    }); 

    response.end("No POST data here!"); // Tell the user that there was no POST data. 
} 

exports.POST = function(query, postdata, request, response) { // Out POST function. 

    var rootnode = undefined; 
    var realdata = undefined; 
    var result = undefined; 

    if(postdata["data"].startsWith("<?xml")) // It's XML, parse it 
    { 
     console.log("  | Detected XML."); 
     var parser = new xml2js.Parser(); // Create an XML parser 
     parser.parseString(postdata["data"], function (err, data) { // Parse the XML from the POST data 
      if(err){ 
       inspect(err); // If we have an error, inspect it with eyes 
      } 
      else 
      { 
       // inspect(result); // Removed; printed the XML data as an array 

       for (var prop in data) { // Get the root node of our XML; this is the command 
        rootnode = prop; 
        break; 
       } 

       realdata = data[rootnode]; // Get the data without the root node 
       result = data; 
      } 
     }); 
    } 
    else // Try to parse it as JSON 
    { 
     console.log("  | Detected JSON."); 
     result = JSON.parse(postdata["data"]); 
     for (var prop in result) { // Get the root node of our JSON; this is the command 
      rootnode = prop; 
      break; 
     } 

     realdata = result[rootnode]; // Get the data without the root node 
    } 
    console.log("  | Before: ") 
    inspect(postdata["data"]); // Inspect the data we've got (XML or JSON) 
    console.log("  | Native object: ") 
    inspect(result); // Inspect the data that's been parsed to an object 
    console.log("  | XML: ") 
    xmldata = jsontoxml.obj_to_xml(result) // Object -> XML 
    xmldata = '<?xml version="1.0" encoding="UTF-8"?><Request>' + xmldata + "</Request>"; // WUPOS extra XML stuff and the XML header 
    inspect(xmldata); // Inspect the XML created from the object 

    response.writeHead(200, { // Write the header 
     'Content-Type': 'text/plain; charset=utf-8', 
     'Content-Length': xmldata.length 
    }); 

    response.write(xmldata); 
} 

А вот внутренний код ответа HTTP ..

var fs = require('fs'); 
var url = require('url'); 
var path = require('path'); 
var querystring = require("querystring") 

var ext = require("./ext.js").ext // For getting MIME types (I know, there's another module for this) 

// Logging function 
function Log(message, prefix, isSecure) 
{ 
    if (!prefix) 
    { 
     prefix = "  "; 
    } 
    else 
    { 
     if (isSecure) 
     { 
      prefix = "HTTPS"; 
     } 
     else 
     { 
      prefix = "HTTP "; 
     } 
    } 

    console.log(prefix + " | " + message); 
} 

exports.Log = Log; 

// httpRequest; this function serves standard HTTP requests 
function httpRequest(request, response, isSecure) { 
    request.setEncoding('utf-8'); // Set the encoding 
    requrl = url.parse(request.url, true); // Parse the URL 
    reqhost = request.connection.address();// Get the IP and port of the user 

    if (requrl.pathname == "/") // If they were requesting the root.. 
    { 
     if (path.existsSync("../html/index.jsp")) // If index.jsp exists.. 
     { 
      reqfile = "/index.jsp"; // Remember that we want that file 
     } 
     else // Otherwise, index.html 
     { 
      reqfile = "/index.html"; 
     } 
     // Log it 
     if (requrl.search) { 
      Log( 
       "[" + reqhost.address + ":" + reqhost.port + "] " + request.method + " " + reqfile + requrl.search + " (Redirected from \"/\")", 
       true, isSecure 
      ); 
     } 
     else { 
      Log( 
       "[" + reqhost.address + ":" + reqhost.port + "] " + request.method + " " + reqfile + " (Redirected from \"/\")", 
       true, isSecure 
      ); 
     } 
    } 
    else // If not, 
    { // Log it, 
     Log( 
      "[" + reqhost.address + ":" + reqhost.port + "] " + request.method + " " + requrl.href 
      , true, isSecure 
     ); 
     reqfile = requrl.pathname; // Remember which file was requested 
    } 



    if (reqfile.endsWith(".jsp")) { // If the file is a JS page 
     try { // Try.. 
      reqjs = require("../html/" + reqfile); // ..to import the code from our script 
      if (reqjs.separateHeader) { // If the script has a separate function for sending the header.. 
       reqjs.headers(request, response); // Send it 
      } 
      if (request.method == 'GET') // If we have a GET 
      { 
       reqjs.GET(requrl.query, request, response); // Run the script's GET function 
      } 
      else if (request.method == 'POST') // If we have a POST 
      { 
       // Grab all the POST data 
       var fullBody = ''; 

       request.on('data', function(chunk) { 
        if(fullBody.length > 1e6) // If we're getting a massive amount of data, kill the connection 
        { 
         Log("POST flood attack/faulty client detected. Connection closed.", false, isSecure); 
         request.connection.destroy(); 
         return; 
        } 
        fullBody += chunk.toString(); 
       }); 

       request.on('end', function() { 
        var postdata = querystring.parse(fullBody); // Parse the POST data 
        if (reqjs.POST) // If the script has a POST function, 
        { 
         reqjs.POST(requrl.query, postdata, request, response); // Call it 
        } 
        else 
        { // Otherwise, just call the GET function 
         reqjs.GET(requrl.query, request, response); 
        } 
       }); 
      } 
     } 
     catch(e) // If there's an error.. 
     { 
      response.writeHead(500, { 
       'Content-Type': 'text/plain' 
      }); 
      response.write("Error: " + e); // Send it to the browser 
      Log("Error: " + e, false, isSecure); // Log it 
     } 
     response.end(); // Finish the response 
    } 
    else // If the file is not a JS page, 
    { 
     fs.readFile("html" + reqfile, function(err, data) { // Read the file in 
      if(err) { // If there's an error.. 
       errortype = err.message.split(",")[0]; // ..get the error's code 
       if (errortype == "ENOENT") // File not found 
       { 
        response.statusCode = 404; 
        response.end("File not found: " + reqfile); // Send them a 404 
        Log("File not found.", false, isSecure); // Log it 
       } 
       else if (errortype == "EISDIR") // File is actually a directory 
       { 
        if (path.existsSync("html" + reqfile + "/index.jsp")) // If there's an index.jsp file here.. 
        { // Redirect the browser 
         Log("Found index.jsp", false, isSecure); 
         response.writeHead(301, "Moved Permanently", { 
          "Location" : reqfile + "/index.jsp" 
         }); 
         response.end("<a href=\"" + reqfile + "/index.jsp" + "\">Please click here.</a>") 
         return; // Return, so we don't have to wrap the next section of code in braces 
        } 
        else if (path.existsSync("html" + reqfile + "/index.html")) // Or, if there's an index.html file here.. 
        { // Redirect the browser 
         Log("Found index.html", false, isSecure); 
         response.writeHead(301, "Moved Permanently", { 
          "Location" : reqfile + "/index.html" 
         }); 
         response.end("<a href=\"" + reqfile + "/index.html" + "\">Please click here.</a>") 
         return; // Return, so we don't have to wrap the next section of code in braces 
        } 
        // If we don't have those files, list them 
        Log("Listing files in html/"+reqfile, false, isSecure); // Log it 

        response.statusCode = 200; // Use Node.js's standard "OK" header 
        // Write out some HTML 
        response.write("<html><head></head><body>\n"); 
        response.write("<h1>Directory listing: " + reqfile + "</h1>\n"); 
        response.write("<ul>\n"); 
        // List off the files 
        var filelist = fs.readdirSync("html" + reqfile); 

        // For every file.. 
        for (element in filelist) 
        { 
         // Compile some HTML 
         var datastr = ""; 
         datastr += "<li>"; 
         datastr += "<a href=\"" + reqfile + "/" + filelist[element] + "\">"; 
         if (filelist[element].endsWith(".jsp") || filelist[element].endsWith(".html")) 
         { // If it ends in html or js, it's a normal page, so colour it green 
          datastr += "<span style=\"color: green;\">"; 
          datastr += filelist[element]; 
          datastr += "</span></a>"; 
         } 
         else 
         { // Otherwise, just put it in the list 
          datastr += filelist[element]; 
          datastr += "</a>"; 
         } 

         datastr += "</li>\n"; 
         response.write(datastr); // Write out the HTML and go around again 
        } 
        response.end("</ul></body></html>"); // Finish the response 
       } 
       else 
       { // There was some other problem when opening the file 
        Log("Could not open file: " + err, false, isSecure); // Log it 
        response.statusCode = 501 // Internal server error code 
        response.end("Could not open file: " + err.message); // Tell the browser 
       } 
      } 
      else 
      { // No problems or anomalies. Serve it! 
       var contenttype = ext.getContentType(ext.getExt(reqfile).replace(".", "")); // Get the MIME type 
       Log("Content-Type: " + contenttype, false, isSecure); // Log it 
       response.writeHead(200, "OK", { 
        'Content-Type': contenttype 
//     'Access-Control-Allow-Origin': 'http://b.localhost:25566', 
//     'Access-Control-Allow-Methods': 'POST, GET', 
//     'Access-Control-Allow-Headers': 'Content-Type' 
       }); 
       response.write(data); // Send the data (TODO: Send in chunks?) 
       response.end() // End 
      } 
     }); 
    } 
} 

exports.httpRequest = httpRequest; 

И код страницы HTML ..

<html> 

<head> 
    <title>JS test A</title> 
    <script src="js/jquery.js"></script> 
</head> 

<body style="margin-left: 30%; margin-right: 30%;"> 
    <div id="tests" style="float:left; width=40%;"> 
     <a href="#" id="a">Test A</a> 
    </div> 

    <div id="output" style="float:right; width=60%;"> 
     <form id="form"> 
      <textarea id="output" name="output"></textarea> 
     </form> 
    </div> 

    <script> 
     $(document).ready(function(){ 
      $("a#a").click(function(event){ 
       text = $("textarea").val(); 
       $("textarea").val(text + "POST test.\n"); 
       text = $("textarea").val(); 
       var http = new XMLHttpRequest(); 

       jsonobj = { 
        array: { 
         obj1: "obj1", 
         obj2: "obj2" 
        }, 
        obj3: "obj3" 
       } 

       var url = "postTest3.jsp"; 
       var params = "data="+JSON.stringify(jsonobj); 
       http.open("POST", url, true); 

       //Send the proper header information along with the request 
       http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

       http.onreadystatechange = function() {//Call a function when the state changes. 
        if(http.readyState == 4 && http.status == 200) { 
         $("textarea").val(text + http.responseText); 
        } 
        else 
        { 
         $("textarea").val(text + "Repsponded: " + http.status + "\n"); 
        } 
       } 
       http.send(params); 

      }); 
     }); 


    </script> 
</body> 

</html> 

EDIT 4: Дополнительный код

{ 
       // Grab all the POST data 
       var fullBody = ''; 

       request.on('data', function(chunk) { 
        if(fullBody.length > 1e6) // If we're getting a massive amount of data, kill the connection 
        { 
         Log("POST flood attack/faulty client detected. Connection closed.", false, isSecure); 
         request.connection.destroy(); 
         return; 
        } 
        fullBody += chunk.toString(); 
       }); 

       request.on('end', function() { 
        var postdata = querystring.parse(fullBody); // Parse the POST data 
        if (reqjs.POST) // If the script has a POST function, 
        { 
         postout = reqjs.POST(requrl.query, postdata, request, response); // Call it 
         if (postout) { 
          inspect(response.write(postout, 'utf8')); 
         } 
        } 
        else 
        { // Otherwise, just call the GET function 
         reqjs.GET(requrl.query, request, response); 
        } 
       }); 
      } 

Есть ли у кого любые идеи по этому поводу?

+0

Когда вы говорите о JSP, вы на самом деле ссылаетесь на JavaScript, верно? Это две совершенно разные вещи. – Amberlamps

+0

Да, извините, я просто назвал свои страницы такими. Они просто JS. – gdude2002

+0

Зачем вам называть ваши файлы JavaScript '.jsp'? Предполагается, что это окончание зарезервировано для страниц сервера Java! – Amberlamps

ответ

0

Итак, я исправил проблему. Думал, что я поделюсь своим решением здесь.

В принципе, после добавления некоторых проверок() было выяснено, что поскольку reponse.write() выполнялся асинхронно, а response.end() не был, сначала вызывался response.end(). Вот почему response.write() возвращает false.

Я установил его, переместив response.end() в мои асинхронные блоки.