简体   繁体   中英

Setting up node-http-proxy for Django/NodeJS app on heroku

I am deploying a Django app that makes use also of nodeJS With that, I have stumbled upon

https://blog.heroku.com/heroku-django-node

I was able to setup the buildpacks and procfiles but I'm having problems with setting up node-http-proxy

So I guess this part confuses me (from the link above):

Of course, still only 1 process on the web dyno can bind to PORT. By adding an environment variable (we called it DJANGO_PORT) and adding node-http-proxy to our node script, we were able to bind node to PORT and proxy all normal web traffic through to Django over 127.0.0.1. The resulting Procfiles look something like this:

I have already added the env variable to my heroku app.

2 Questions

  • Is this how you properly bind the PORT in server.js? I have:
 const PORT = process.env.PORT httpProxy.createProxyServer({target:'app_url:8080'}).listen(PORT); 
  • 8080 is my DJANGO_PORT, I am expecting this would also route traffic to my django server?

I am getting this error with the above:

    /app/node_modules/http-proxy/lib/http-proxy/index.js:119
     throw err;
     ^

 Error: connect ECONNREFUSED {ip_address}:8080
     at Object._errnoException (util.js:1022:11)
     at _exceptionWithHostPort (util.js:1044:20)
     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14)

Need help, at least to know if my idea or understanding is correct. Just want to know if I'm on the right direction of thinking

Thanks in advanced!

Got it solved

For anyone who wants to run Django and NodeJS on a single dyno on heroku.

Here's what I've done

I added the custom buildpack to my app via Heroku CLI along with heroku/python and heroku/nodejs (multiple buildpacks)

http://www.github.heroku.com/heroku/heroku_buildpack_runit

Created Procfile

web: bin/runsvdir-dyno

and Procfile.web where DJANGO_PORT is on Heroku's Envs

django: gunicorn appname.wsgi:application --bind 127.0.0.1:$DJANGO_PORT
node: node server.js

Finally used node-http-proxy as follows. Here's mine with a node app running on port 3000

var webpack = require('webpack')
var WebpackDevServer = require('webpack-dev-server')
var config = require('./webpack.local.config')
var httpProxy = require('http-proxy')
const PORT = process.env.PORT
// ::::::::::::::::::Starting from here
var NODE_PORT = 3000;
var http = require('http')
var proxy = httpProxy.createProxyServer({});

http.createServer(function(req, res) {
    // For all URLs beginning with /channel proxy to the Node.JS app, for all other URLs proxy to the Django app running on DJANGO_PORT
    if(req.url.indexOf('/channel') === 0) {
        // Depending on your application structure you can proxy to a node application running on another port, or serve content directly here
        proxy.web(req, res, { target: 'http://localhost:' + NODE_PORT });

        // Proxy WebSocket requests if needed
        proxy.on('upgrade', function(req, socket, head) {
            proxy.ws(req, socket, head, { target: 'ws://localhost:' + NODE_PORT });
        });
    } else {
        proxy.web(req, res, { target: 'http://localhost:' + process.env.DJANGO_PORT });
    }
}).listen(process.env.PORT);
// :::::::::::::::::::To HERE
new WebpackDevServer(webpack(config), {
  publicPath: config.output.publicPath,
  hot: true,
  inline: true,
  historyApiFallback: true,
  watchOptions: {
    aggregateTimeout: 300,
    poll: 1000
  },
  headers: { "Access-Control-Allow-Origin": "*" },
}).listen(3000, config.ip, function (err, result) {
  if (err) {
    console.log(err)
  }

  console.log('Listening at ' + config.ip + ':3000')
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM