如何在 Heroku 的同一个测功机上 运行 Django 和 node.js 应用程序?

How to run Django and node.js app on same dyno in Heroku?

我在 Heroku 上部署了一个 Django 应用程序,但由于某些要求,我必须创建一个基本的 nodejs 文件,我希望 运行 它在同一个 dyno 上。 我在谷歌上搜索了解决方案,但除了博客之外找不到很多其他解决方案,它们都是一样的 https://blog.heroku.com/heroku-django-node 所以,我确实安装了提到的 buildpack,我的配置文件是: 简介:

web: bin/runsvdir-dyno

Profile.web

django: gunicorn exampledjangoapp.wsgi:application --bind 127.0.0.1:$DJANGO_PORT
node: node node_app/app.js

我也在 Heroku 中创建了 NODE_ENV 和 DJANGO_PORT 配置。 这是我在日志中看到的内容:

 Error: listen EADDRINUSE :::56842
2017-05-05T21:02:15.241005+00:00 app[web.1]:     at Object.exports._errnoException (util.js:1022:11)
2017-05-05T21:02:15.241006+00:00 app[web.1]:     at exports._exceptionWithHostPort (util.js:1045:20)
2017-05-05T21:02:15.241007+00:00 app[web.1]:     at Server._listen2 (net.js:1259:14)
2017-05-05T21:02:15.241007+00:00 app[web.1]:     at listen (net.js:1295:10)
2017-05-05T21:02:15.241008+00:00 app[web.1]:     at Server.listen (net.js:1391:5)
2017-05-05T21:02:15.241009+00:00 app[web.1]:     at Object.<anonymous> (/app/node_app/app.js:15:4)
2017-05-05T21:02:15.241009+00:00 app[web.1]:     at Module._compile (module.js:570:32)
2017-05-05T21:02:15.241010+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:579:10)
2017-05-05T21:02:15.241011+00:00 app[web.1]:     at Module.load (module.js:487:32)
2017-05-05T21:02:15.241011+00:00 app[web.1]:     at tryModuleLoad (module.js:446:12)
2017-05-05T21:02:15.285970+00:00 app[web.1]: buildpack=runit ps=node at=exit status=1 waitpid_lsb=0
2017-05-05T21:02:15.305915+00:00 app[web.1]: buildpack=runit ps=node at=start
2017-05-05T21:02:16.126572+00:00 app[web.1]: events.js:160
2017-05-05T21:02:16.126582+00:00 app[web.1]:       throw er; // Unhandled 'error' event
2017-05-05T21:02:16.126583+00:00 app[web.1]:       ^
2017-05-05T21:02:16.126584+00:00 app[web.1]: 
2017-05-05T21:02:16.126584+00:00 app[web.1]: Error: listen EADDRINUSE :::56842
2017-05-05T21:02:16.126585+00:00 app[web.1]:     at Object.exports._errnoException (util.js:1022:11)
2017-05-05T21:02:16.126586+00:00 app[web.1]:     at exports._exceptionWithHostPort (util.js:1045:20)
2017-05-05T21:02:16.126587+00:00 app[web.1]:     at Server._listen2 (net.js:1259:14)
2017-05-05T21:02:16.126587+00:00 app[web.1]:     at listen (net.js:1295:10)
2017-05-05T21:02:16.126588+00:00 app[web.1]:     at Server.listen (net.js:1391:5)
2017-05-05T21:02:16.126589+00:00 app[web.1]:     at Object.<anonymous> (/app/node_app/app.js:15:4)
2017-05-05T21:02:16.126589+00:00 app[web.1]:     at Module._compile (module.js:570:32)
2017-05-05T21:02:16.126590+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:579:10)
2017-05-05T21:02:16.126591+00:00 app[web.1]:     at Module.load (module.js:487:32)
2017-05-05T21:02:16.126591+00:00 app[web.1]:     at tryModuleLoad (module.js:446:12)
2017-05-05T21:02:16.154176+00:00 app[web.1]: buildpack=runit ps=node at=exit status=1 waitpid_lsb=0
2017-05-05T21:02:16.287874+00:00 heroku[web.1]: Process exited with status 111
2017-05-05T21:02:16.301460+00:00 heroku[web.1]: State changed from starting to crashed

我的app.js是:

var http = require('http'),
    httpProxy = require('http-proxy');
//
// Create your proxy server and set the target in the options.
//
httpProxy.createProxyServer({target:'https://example.herokuapp.com/channel'}).listen(process.env.PORT); // See (†)

//
// Create your target server
//
http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(process.env.PORT);

所以,我基本上想做的是,在 http://example.herokuapp.com to be Django app, and http://example.herokuapp.com/channel 上指向 nodejs 应用程序。 有什么建议吗?

Error: listen EADDRINUSE 错误来自于在 app.js.

中使用同一端口调用 .listen() 两次

你走在正确的轨道上。下面是一个示例,说明如何将 /channel 请求代理到 Node.JS 应用程序并将所有其他请求代理到另一个应用程序(在本例中为 Django 应用程序):

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

var NODE_PORT = 4711;

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);

// Example node application running on another port
http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.write('Request successfully proxied to Node.JS app');
    res.end();
}).listen(NODE_PORT);