一直在OpenShift上托管的Node.js静态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或route方法内部调用myCallback函数似乎不起作用。我正在使用Cloud9 IDE并直接将git push推送到OpenShift来进行此项目,因此我在调试时遇到了一些困难,但尝试了许多不同的方法,但均未成功,包括设置request.setTimeout函数为该程序提供了一些时间。计时器/流程事件触发。我当前的OpenShift应用正在运行Node.js 0.6。是否有任何显而易见的问题可能导致我可能会丢失的问题?
最佳答案
我这样做可以让您的setTimeout工作:
注释掉“ response.setTimeout(500);”在第54行。无效。
注释掉“((function(){route(pathname,query,myCallback)})();”在第71行。也无效。
将第76行的超时时间更改为5000(5000ms = 5秒)
为了使nextTick工作:
到处都只做“ process.nextTick”而不是“ util.process.nextTick”。
将第16行更改为:“ returnValue = add(query ['num1'],query ['num2'])。toString();” (必须将其转换为字符串!)
取消注释17、18、19即可看到此功能
注释掉第54行,您不需要这个
将第70行更改为“ route(pathname,query,myCallback);”
您应该看到自己现在做错了。