2012-10-15 42 views
1

我有兩個節點服務器,另一個在端口5000(稱之爲「臉」),另一個在端口5001(稱之爲「手」)無法獲取一個的node.js服務器去跟另一個

兩個通過一個工頭procfile在同一時間啓動。端口是固定的,我的目標網址在瀏覽器中工作。

當雙手啓動時,它需要與Face(Facepalm?)交談並註冊。但是,下面的代碼似乎沒有工作。 (這是coffeescript生成的JS)

在服務器初始化期間,http服務器啓動後,調用註冊表。如果是定時問題,我用2秒的setTimeout()啓動註冊函數。我知道它打的頁面(/ home/register)可用並正在工作。

現在我可以看到它到達「發佈到」控制檯日誌行。在面對面我已經在註冊碼中放置了一個console.log,並且它從不記錄任何東西 - 這意味着我不認爲它實際上受到了打擊。 (如果從瀏覽器中點擊,它會記錄日誌)並且沒有任何錯誤 - 它只是調用請求,然後漫步得到一個三明治。

這兩個服務器都是「自己動手」 - 不使用任何框架。如果您看到一個奇怪的錯字或需要更多信息,請告訴我。謝謝!

register = function() { 
    var _this = this; 
    console.log('Registering with Face Server'); 
    return post_face('/home/register', GLOBAL.data, function(rs) { 
     console.log(rs); 
     if (rs.registered) { 
     GLOBAL.data.registered = true; 
     return console.log("Registered with face at " + GLOBAL.config.face_host + ":" + GLOBAL.config.face_port); 
     } else { 
     throw "ERROR: Could not register with face server! " + GLOBAL.config.face_host + ":" + GLOBAL.config.face_port; 
     return false; 
     } 
    }); 
    }; 

    post_face = function(path, data, cb) { 
    var post_data, post_options, post_req; 
    post_data = querystring.stringify({ 
     'registration': data 
    }); 
    post_options = { 
     host: GLOBAL.config.face_host, 
     port: GLOBAL.config.face_port, 
     path: path, 
     method: 'POST', 
     headers: { 
     'Content-Type': 'application/x-www-form-urlencoded', 
     'Content-Length': post_data.length 
     } 
    }; 
    console.log("Posting to " + post_options.host + ":" + post_options.port); 
    post_req = http.request(post_options, function(res) { 
     var response, 
     _this = this; 
     console.log(res); 
     response = ""; 
     res.setEncoding('utf8'); 
     res.on('data', function(chunk) { 
     return response += chunk; 
     }); 
     return res.on('end', function() { 
     return cb.call(_this, response); 
     }); 
    }); 
    return true; 
    }; 
+0

你的意思是用'post_data'做某事嗎? – Bill

+0

感嘆。好決定。我現在意識到http.request的實例並不實際發佈數據,我需要添加: post_req.write(post_data); post_req.end(); 謝謝! –

回答

0

感謝上面的Bill,答案是我實際上沒有發佈數據並結束請求!我指的是一些樣本的錯誤複製/粘貼/編輯。下面是我應該有的最後兩行代碼:

post_req.write(post_data); 
post_req.end(); 
相關問題