2017-05-06 80 views
0

我在Heroku上部署了一個Django應用程序,但由於某些要求,我必須創建一個基本的nodejs文件,並且希望在同一個測試程序中運行它。 我用Google搜索解決方案,但不可能拿出從博客,而這一切都是相同的https://blog.heroku.com/heroku-django-node 這麼多分開,我沒有安裝buildpack提到的,和我的配置是: Procfile:如何在Heroku中的相同dyno上運行Django和node.js應用程序?

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是Django應用程序,並http://example.herokuapp.com/channel點到nodejs應用程序。 有什麼建議嗎?

+0

這是混亂的,都是節點和DJANGO偵聽端口80/443?看起來好像你知道你需要從django代理節點或節點到Django,但是'Error:listen EADDRINUSE ::: 56842'使它看起來像不是這樣。 –

+0

@RobOsborne不,他們被設置爲在不同的端口上運行,我在Heroku上設置了DJANGO_PORT和NODE_ENV端口環境變量,並將其設置爲在不同的端口上運行。代理節點是我遇到麻煩的地方,我猜。至於如何去做,我真的無能爲力。 – Maverick

回答

1

Error: listen EADDRINUSE錯誤來自調用.listen()兩次,使用app.js中的相同端口。

您正處在正確的軌道上。下面是如何向代理/信道請求發送到應用程序的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); 
+0

工程像魅力! WS是我想要這樣的設置的唯一原因!謝謝! – Maverick

相關問題