简体   繁体   English

在heroku上为Django / NodeJS应用设置node-http-proxy

[英]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 我正在部署一个也使用nodeJS的Django应用程序,我偶然发现

https://blog.heroku.com/heroku-django-node 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 我能够设置buildpacks和procfile,但是在设置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. 当然,Web dyno上仍然只有1个进程可以绑定到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. 通过添加环境变量(我们将其称为DJANGO_PORT)并将node-http-proxy添加到我们的节点脚本中,我们能够将节点绑定到PORT,并将所有正常的网络流量代理到127.0.0.1之上的Django。 The resulting Procfiles look something like this: 产生的Procfiles看起来像这样:

I have already added the env variable to my heroku app. 我已经将env变量添加到我的heroku应用程序中。

2 Questions 2个问题

  • Is this how you properly bind the PORT in server.js? 这是您如何正确绑定server.js中的PORT吗? 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? 8080是我的DJANGO_PORT,我希望这也会将流量路由到我的django服务器吗?

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. 对于想在heroku上的单个dyno上运行Django和NodeJS的任何人。

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) 我通过Heroku CLI将自定义buildpack连同heroku / python和heroku / nodejs(多个buildpack)一起添加到了我的应用程序中

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

Created Procfile 创建的Procfile

web: bin/runsvdir-dyno

and Procfile.web where DJANGO_PORT is on Heroku's Envs Procfile.web ,其中DJANGO_PORT在Heroku的环境中

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

Finally used node-http-proxy as follows. 最后如下使用node-http-proxy。 Here's mine with a node app running on port 3000 这是我的在端口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')
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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