Node-http-proxy: Error in http-proxy module : Must provide a proper URL as target

Created on 14 Dec 2016  路  3Comments  路  Source: http-party/node-http-proxy

I'm new in Node.JS and deployed the first application on VPS. After running on port 8000, i decided create a http-proxy for forward each domain to its specific port . I worte a little application like here :

var http = require('http'),
httpProxy = require('http-proxy');

var option = {
router : {
'domain.com' : 'http://88.198.86.100:8000'
}
};

var proxyServer = httpProxy.createServer(option);
proxyServer.listen(80);
88.198.86.100 is my server ip.

So, my problem here is shown , when i typed 88.198.86.100 in my browser PC (Google Chrome), my proxy application in server was carshed and gave this error :

C:\Users\Administrator\Desktop\Nodejs\node_modules\http-proxy\lib\http-proxy\index.js:119
throw err;
^

Error: Must provide a proper URL as target
at ProxyServer. (C:\Users\Administrator\Desktop\Nodejs\node_modules\http-proxy\lib\http-proxy\index.js:68:35)
at Server.closure (C:\Users\Administrator\Desktop\Nodejs\node_modules\http-proxy\lib\http-proxy\index.js:125:43)
at emitTwo (events.js:106:13)
at Server.emit (events.js:191:7)
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:546:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
error: Forever detected script exited with code: 1

I want to someone enter IP server into the each browser, my application will not crash.

Most helpful comment

Hi,

Do you find a solution? I have same problem.

All 3 comments

Hi,

Do you find a solution? I have same problem.

Im having the same issue. Looking for issue/work around.

I can proxy to http://1.1.1.1:8096/ but not to http://1.1.1.1:8080/.

The difference?
port 8096 = Emby media server -- https://emby.media/download.html
port 8080 = Pea Server -- https://github.com/recap/pea-server

Perhaps this is related to what the proxy receives as the response from the target?

Digging further I found that as of http-proxy/lib/http-proxy/index.js:60 i cannot find the target.
console.log(options.target); //was undefined
console.log(options['target']); //was undefined
console.log(options[0]); //was undefined

MUCH ODDNESS! -- cleared my browser cache, moved the target out of setting it directly and instead pulled it from my dataset as i planned and somehow go this to work. I will post my code below for anyone wanting to see how i got it working.
app.db:

{"data":[{"table":"routes","name":"awe","authenticate":"true","path":"app","target":"http://1.1.1.1:8080/","_id":"ee3e2439-ef94-4f45-a702-faf00c2632e1"}

server.js:

var express = require('express');
var httpProxy = require('http-proxy');
var app = require('./app-lib.js')
var proxy = httpProxy.createProxyServer();
var web = express();
web.set('views','./views');
web.set('view engine','jade');

web.all('*',function(req,res){
  path=req.path.split('/').slice(1)[0];
  app.getTargetForPath(path,function(target){
  if(target){
    //proxy.web(req,res,{target:target,ws:true,xfwd:true,autoRewrite:true});  //do not pass path to proxy (ie path -> target)
    proxy.web(req,res,{target:target,ws:true,xfwd:true}); //pass path to proxy target (ie path -> target/path)
  }else{
    res.redirect('/routeUndefined');
  };
  });
});

app-lib.js:

var db = require('./app-db.js');
function getTargetForPath(path,callback){
  db.getRouteByPath(path,function(route){
  if(route){
    callback(route.target);
  }else{
    callback(null);
  };
  });
}
exports.getTargetForPath=getTargetForPath

app-db.js:

function getRouteByPath(routePath,callback){
  db.find({table:'routes',path:routePath},function(err,route){
  if(err){
    log(1,'noRouteForPath:'+routePath);
    callback(null);
  }else{
    log(0,'gotRouteForPath:'+routePath);
    callback(route[0]);
  }
  });
};
exports.getRouteByPath=getRouteByPath

I had this issue when I accidentally called createServer on httpProxy instead of http.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DominicTobias picture DominicTobias  路  3Comments

guillaume-at-palo picture guillaume-at-palo  路  4Comments

damianchojna picture damianchojna  路  5Comments

adi518 picture adi518  路  3Comments

lrettig picture lrettig  路  4Comments