Node.js回调问题

时间:2013-06-16 21:21:35

标签: javascript node.js openshift cloud9-ide

正在处理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方法,那么我会得到一个没有任何问题的响应;但是,使用process.nextTick或setTimeout调用onRequest或路由方法中的myCallback函数似乎不起作用。我正在使用Cloud9 IDE进行这个项目,直接git推送到OpenShift,所以我在调试时遇到了一些困难,但尝试了很多不同的方法但没有成功,包括设置request.setTimeout函数为它提供一些时间要触发的计时器/进程事件。我当前的OpenShift应用程序正在运行Node.js 0.6。是否有任何明显可能导致我可能会遗漏的问题?

1 个答案:

答案 0 :(得分:2)

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

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

让nextTick工作:

  • 到处都只有“process.nextTick”而不是“util.process.nextTick”。
  • 将第16行更改为:“returnValue = add(query ['num1'],query ['num2'])。toString();” (必须将其作为字符串投射!)
  • 取消注释17,18,19,看这将会有效
  • 注释掉第54行,你不需要这个
  • 将第70行更改为“route(pathname,query,myCallback);”

你应该看看你现在做错了什么。