跳到主要内容
CNode

用node做一个http代理服务器的问题

社区
DDysonL发布于 12 年前最后回复 12 年前
5234220

我做的这台代理服务器作为网关,把来自公网客户端的请求通过http转发到内部的连天服务器,但在开发阶段由于客户端发送带未转义的双引号,换行符等字符,导致聊天服务器解析json数据包出错,从而未响应我的网关服务器。

我于是设置了请求超时,和错误事件处理,以结束客户端的请求。但即使这样,node写的网关服务器还是经常被挂起,即进程和端口一直在,但就是不能再响应来自客户端的请求了,最后返回server no reult。初次使用nodejs写,没有系统学习过,请大神帮忙看看我这代码有啥问题。


process.on('uncaughtException', function (err) {
  console.log('Caught exception: ' + err);
});

var config=require('./config.json');
if(config){
  for(var k in config){
    console.log(k+':'+config[k]);
  }
}
var LOCAL_PORT = config.localPort || 1337;
var REMOTE_PORT = config.mailchatServerPort || 7777;
var REMOTE_ADDR = config.mailchatServer || '10.25.2.17';

var log4js = require('log4js');
log4js.configure({
  appenders: [
    { type: 'console' },
    //{ type: 'file', filename: 'logs/cheese.log', category: 'cheese' },
    { type: 'dateFile', filename: 'info.log', pattern: '-yyyy-MM-dd', alwaysIncludePattern: false, category: 'dateFileLog' }
  ],
  replaceConsole:true,
  levels:{
      dateFileLog: 'INFO'
  }
});

var log = log4js.getLogger('dateFileLog');


http = require('http');
http.createServer(onRequest).listen(LOCAL_PORT);
log.info('Gateway Server listen at ' + LOCAL_PORT + '.');

function onRequest(client_req, client_res) {
  if(client_req.url.indexOf('/favicon.ico')==0) return;
  log.info('Request URL: ' + client_req.url);
  log.info('Request Header: ' + JSON.stringify(client_req.headers));
  var options = {
    hostname: REMOTE_ADDR,
    port: REMOTE_PORT,
    path: client_req.url,
    method: client_req.method,
    headers: client_req.headers
  };
  client_res.setHeader('Content-type','application/json');
  client_res.setHeader('Access-Control-Allow-Origin','*');
  var proxy = http.request(options, function (res) {
    log.info('From ChatServer STATUS: ' + res.statusCode);
    log.info('From ChatServer HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('UTF8');
    res.pipe(client_res, {
      end: true
    });
  });

  proxy.on('error', function(e) {
    log.info('Problem with request from ChatServer: ' + e.message);
    client_res.statusCode=500;
    client_res.end();
    //client_res.end('cache server error: '+e.message);
  });

 proxy.setTimeout(10*1000, function(){
   log.info('Request Timeout From ChatServer in 10 seconds, then response code 500');
   client_res.statusCode=500;
   client_res.end();
   //client_res.end('Reuqest Timeout');
 });

 client_req.setTimeout(10*1000, function(){
   log.info('Timeout');
   client_res.statusCode=500;
   client_res.end();
 });

  client_req.pipe(proxy, {
    end: true
  });

}

查看回复

回复 (5)

H
hackerjs#312 年前

var proxy = function () { //封装异步请求,其余的你自己改试试,主要问题应该是没处理好node.js 的异步问题,好久没做Node.js开台开发,最近在研究JS本身。 http.request(options, function (res) { log.info('From ChatServer STATUS: ' + res.statusCode); log.info('From ChatServer HEADERS: ' + JSON.stringify(res.headers)); res.pipe(client_res, { end: true }); }); }; client_req.on('end', proxy); 建议用责任链模式来设计,维护起来更方便 .

T
tulayang#212 年前

好长,不看了。 不要用node做代理,用nginx。 node毕竟是js写逻辑,不如nginx的c语言更底层,而且很现成方便的东西,不用岂不是很可惜。 而且两个小时你就可以学会并搭建一个nginx代理服务器。

另外,即便node做服务器,要有一个文件专门写好路由映射规则,逻辑细节和规则分开会比较容易调试。

Z
zhe feng#112 年前

干嘛自己写,用这个https://github.com/nodejitsu/node-http-proxy

nodejitsu官方用的。自己写太麻烦了

参与回复

登录后即可参与回复。登录