Node.js回调问题

Node.js Callback Issues

本文关键字:问题 回调 js Node      更新时间:2023-09-26

一直致力于OpenShift上托管的Node.js restful web服务。目前,我已经成功地使用了简单的方法调用等,但似乎无法通过异步回调获得http响应。

以下是我目前的记录:

var http = require("http");
var url = require("url"); // used to get the requested method name as well as parameters
var util = require("util");
// global variables

// router function
function route(pathname, query, callbackFunc) {
  //return executeMethod(pathname, query);
  var returnValue;
  switch (pathname.toUpperCase()) {
    case "/ADD":
        returnValue = add(query['num1'], query['num2']);
        //util.process.nextTick(function() {
        //callbackFunc(null, returnValue);
        //});
        break;
    case "/ADDASYNC":
        //addAsync(query['num1'], query['num2'], callback);
        break;   
    default:
        returnValue = "method not found";
        break;
  }
  //return returnValue;
  //return "Route for request " + pathname + " complete, query: " + query;
}

// actual web method execution
function add(num1, num2){
    //return "add method called with values: " + num1 + " " + num2;
    return parseFloat(num1) + parseFloat(num2);
}
function addAsync(num1, num2, callback){
    //util.process.nextTick(function(){
    //    var value = parseFloat(num1) + parseFloat(num2);
    //    util.process.nextTick(function(){
    //        callback(value);
    //    });
    //});
}
// main request handler for server
function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;
    var query = url.parse(request.url, true).query;
    console.log("Request for " + pathname + " Recieved");
    response.setTimeout(500);
    var myCallback = function(err, data){
        if(err){
            response.writeHead(200, {"Content-Type": "text/plain"});
            response.write('an error occured with requested method');
            response.end();
        }else{
            response.writeHead(200, {"Content-Type": "text/plain"});
            response.write(data);
            response.end();
        }
    }
    //var finalValue = route(pathname, query);
    //var finalValue = 0;
    (function(){route(pathname, query, myCallback)})();
    response.writeContinue();
    //process.nextTick(myCallback(null, 'hello world'));
    setTimeout(function(){
        myCallback(null, "hello world");
    }, 15);
    //myCallback();
    //response.writeHead(200, {"Content-Type": "text/plain"});
    //response.write("Hello World. You requested: " + pathname + " with type " + pathname.type +  ", value: " + finalValue);
    //response.end();
}
// create the server and signal console of start
http.createServer(onRequest).listen(8080, process.env.OPENSHIFT_INTERNAL_IP);
// for debug
//http.createServer(onRequest).listen(process.env.PORT, process.env.IP);
console.log("Server has started. Listening to port: " + 8080 + " ip address: " + process.env.OPENSHIFT_INTERNAL_IP);

如果我在onRequest方法中直接调用myCallback方法,那么我得到一个没有任何问题的响应;但是,调用onRequest或route方法中的myCallback函数使用process。nextTick或setTimeout似乎不起作用。我正在使用Cloud9 IDE和直接git推送到OpenShift来做这个项目,所以我在调试时遇到了一些困难,但我尝试了很多不同的方法都没有成功,包括设置请求。setTimeout函数,为定时器/进程事件的触发提供一些时间。我目前的OpenShift应用程序正在运行Node.js 0.6。有没有什么明显的东西可能导致问题,而我可能忽略了?

我让你的setTimeout工作通过这样做:

    注释掉第54行中的"response.setTimeout(500);"。它是无效的。
  • 注释掉第71行"(function(){route(pathname, query, myCallback)})();"也无效。
  • 将76行超时时间更改为5000 (5000ms = 5秒)

让nextTick工作:

  • 到处只做"process"。
  • 将第16行改为:"returnValue = add(query['num1'], query['num2']).toString();"(必须将其转换为字符串!)
  • 取消注释17,18,19,看看它现在可以工作了
  • 注释掉第54行,你不需要这个
  • 将第70行改为"route(pathname, query, myCallback);"

你应该知道你做错了什么