1

I'm a newbie to node-http-proxy module.

my aim

I need to use the module provide multi-SSL for multi-subdomain. For example; if a user call process.localhost:1443 then I should route the call to process.localhost:2443 and if a user call api.localhost:1443 then I should route the call to api.localhost:3443

what's happening

I wrote the below server.js codes. However when I try to call process.localhost:1443 I get the following error;
D:\Work Space\...\http-proxy\node_modules\requires-port\index.js:13 protocol = protocol.split(':')[0]; TypeError: Cannot call method 'split' of undefined

protocol seems as undefined.

function required(port, protocol) {
  protocol = protocol.split(':')[0];

What should I do?

server.js

var fs = require('fs'),
    httpProxy = require('http-proxy'),
    express = require('express'),
    app = require('./app').service,
    api = require('./api').service;

// PROXY
var options = {
  changeOrigin: true,
  forward: {
    'process.localhost': 'process.localhost:2443',
    'api.localhost' : 'api.localhost:3443'
  }
}

httpProxy.createServer(options).listen(1443, function() {
  console.log('Proxy is listening on port 1443')
})


// HTTP
app
.listen(2443, function() {
  console.log('PROCESS APP server is listening on port 2443')
})

api
.listen(3443, function() {
  console.log('API APP server is listening on port 3443')
})
efkan
  • 12,991
  • 6
  • 73
  • 106
  • for one, you have an extra quote in httpProxy.createServer. also i've never seen subdomains used with localhost – Plato Apr 06 '15 at 15:02
  • Thanks Plato. I thought I should only one server to listen `1443` port. And then I can distribute the request by `hostname` . But I didn't use `hostname`. `targets` aren't enough? – efkan Apr 06 '15 at 15:18
  • Thanks @apsillers, so I shouldn't try on `localhost`. Should I? I couldn't find properly any different way to route ports. – efkan Apr 06 '15 at 15:46
  • @user3765109 Could you clarify which version of `http-proxy` you're running? I looked at the examples on https://github.com/nodejitsu/node-http-proxy and https://www.npmjs.com/package/http-proxy but none of them use `target` the way you're using it. In the examples, `target` is either a string or an object with `host` and `port` properties (`target: { host: ..., port: ... }`) , not a mapping of one host+port to another. – apsillers Apr 06 '15 at 18:15
  • I've found another legacy example http://stackoverflow.com/a/17846415/3765109 And I updated my question according to recent `http-proxy` codes. But it doesn't work.. – efkan Apr 07 '15 at 07:25

1 Answers1

0

I could solve this issue through someone from node-http-proxy forum.

var proxyTable = {}
proxyTable['api.localhost:1443'] = 'http://127.0.0.1:3443'
proxyTable['process.localhost:1443'] = 'http://127.0.0.1:2443'

var proxy = httpProxy.createServer({changeOrigin: true})

var http = require('http')
http.createServer(function(req, res) {

  var options = {
    target: proxyTable[req.headers.host]
  }

  proxy.web(req, res, options)

}).listen(1443, function() {
  console.log('Proxy server is listening on port 1443')
})

app.listen(2443, function() {
  console.log('APP server is listening on port 2443')
})

api.listen(3443, function() {
  console.log('API server is listening on port 3443')
})
efkan
  • 12,991
  • 6
  • 73
  • 106