Need to proxy an existing server (Node / Express) | بلاگ

Need to proxy an existing server (Node / Express)

تعرفه تبلیغات در سایت

آخرین مطالب

امکانات وب

Vote count: 0

I am fairly new to this, so my explanation will probably be terrible as I don't fully understand what is happening.

I am building a webapp that utilizes both Mongo and Ethereum on the backend. I need to utilize some sort of proxy or else the Ethereum node will get a "port already in use" error.

Currently, my express server is listening on port 3000 and I am using browser-sync to "Proxy an EXISTING vhost. Browsersync will wrap your vhost with a proxy URL to view your site."

So my browser-sync config looks like:

browserSync.init(null, {
  proxy: "http://localhost:3000",
  files: [folder.src + "**/*"],
  port: 7000

And now I visit localhost:7000 and everything works properly (if I visit localhost:3000 then only the Mongo backend works but not the Ethereum).

I don't want to use browser-sync in production, so I need to be able to do this same thing in my app.js file.

A couple things I have tried:


var httpProxy = require('http-proxy');
httpProxy.createProxyServer({target:'http://localhost:3000', changeOrigin: true}).listen(7000); 


var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();
app.all("/", function(req, res){ 
  apiProxy.web(req, res, { target: 'http://localhost:7000', changeOrigin: true });


var proxy = require('express-http-proxy');
var apiProxy = proxy('localhost:7000', {
  forwardPath: function (req, res) {
    return require('url').parse(req.url).path;
app.use("/", apiProxy);*/
asked 38 secs ago

نویسنده : استخدام کار بازدید : 6 تاريخ : يکشنبه 5 فروردين 1397 ساعت: 5:27