Nodejs通过url发送值

时间:2012-02-22 10:08:44

标签: javascript node.js rabbitmq

我正在运行带有rabbitmq的nodejs示例这是我在节点上的代码

var http = require('http');
var url = require('url');
var fs = require('fs');
var io = require('socket.io');
var context = require('rabbit.js').createContext();

var httpserver = http.createServer(handler);

var socketioserver = io.listen(httpserver);

socketioserver.sockets.on('connection', function(connection) {
  var pub = context.socket('PUB');
  var sub = context.socket('SUB');

  connection.on('disconnect', function() {
    pub.destroy();
    sub.destroy();
  });

  // NB we have to adapt between the APIs
  sub.setEncoding('utf8');
  connection.on('message', function(msg) {
    pub.write(msg);
  });
  sub.on('data', function(msg) {
    connection.send(msg);
  });
  sub.connect('chat');
  pub.connect('chat');
});

httpserver.listen(8080, '0.0.0.0');

// ==== boring detail

function handler(req, res) {
  var path = url.parse(req.url).pathname;   
  switch (path){
  case '/':
    path = '/index.html';
  case '/index.html':
    fs.readFile(__dirname + '/index.html', function(err, data){
      if (err) return send404(res);
      res.writeHead(200, {'Content-Type': 'text/html'});
      res.write(data, 'utf8');
      res.end();
    });
    break;
  default: send404(res);
  }
}

function send404(res){
  res.writeHead(404);
  res.write('404');
  res.end();
}

问题是这个

 sub.connect('chat');
  pub.connect('chat');

而不是'chat'(兔子上的消息队列名称)我需要将html页面(订阅者)的url中的参数传递给节点。例如,

 sub.connect(myparam);
      pub.connect(myparam);

0 个答案:

没有答案