i have been trying everything but it keeps throwing this error in the log. and says 502 bad gateway when trying to access the url.
我一直在尝试一切,但它不断在日志中抛出此错误。并且在尝试访问URL时说502坏网关。
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at Server._listen2 (net.js:1221:19)
at listen (net.js:1270:10)
at net.js:1379:9
at nextTickCallbackWith3Args (node.js:453:9)
at process._tickCallback (node.js:359:17)
at Function.Module.runMain (module.js:443:11)
at startup (node.js:139:18)
at node.js:968:3
here's my app.js (also my start script as specified in package.json).
这是我的app.js(也是我在package.json中指定的启动脚本)。
var app = require('express')();
var http = require('http').Server(app);
var path = require('path');
var io = require('socket.io')(http);
var routes = require('./routes/home');
var users = require('./routes/users');
io.on('connection', function(socket) {
console.log('a user connected');
socket.on('message', function (data) {
console.log(data);
io.emit('msg', data);
});
});
http.listen(process.env.port, '0.0.0.0', function(err) {
console.log('server runninng at ' + http.url );
});
although at console(stdout) it says -> starting app -> running server at undefined
虽然在控制台(stdout)它说 - >启动应用程序 - >在undefined运行服务器
3 个解决方案
#1
1
You should update the process.env.port
to process.env.PORT
since env vars are case sensitive.
您应该将process.env.port更新为process.env.PORT,因为env变量区分大小写。
The listen should look like this:
听起来应该是这样的:
http.listen(process.env.PORT, '0.0.0.0', function(err) {
console.log('server runninng at ' + http.url );
});
#2
1
Experiment with your code to see what is causing the problem. First try:
试验您的代码,看看导致问题的原因。第一次尝试:
http.listen(4000, '0.0.0.0', function(err) {...
This will solve the problem if it was something to do with your process.env.PORT
variable.
如果它与您的process.env.PORT变量有关,这将解决问题。
If that doesn't work, try:
如果这不起作用,请尝试:
http.listen(4000, function(err) {...
This will solve the problem if it was something to do with the provided hostname (0.0.0.0
). The hostname argument in the listen function is optional, so it should be fine without.
如果它与提供的主机名(0.0.0.0)有关,这将解决问题。 listen函数中的hostname参数是可选的,所以没有它应该没问题。
#3
0
0.0.0.0 is not a valid address to listen to. Try 127.0.0.1 to listen to the loop back on your local machine or get your machine IP address instead.
0.0.0.0不是要侦听的有效地址。尝试使用127.0.0.1在本地计算机上回听环路,或者获取计算机IP地址。
0.0.0.0 means all ports for routing usually.
0.0.0.0表示通常用于路由的所有端口。
#1
1
You should update the process.env.port
to process.env.PORT
since env vars are case sensitive.
您应该将process.env.port更新为process.env.PORT,因为env变量区分大小写。
The listen should look like this:
听起来应该是这样的:
http.listen(process.env.PORT, '0.0.0.0', function(err) {
console.log('server runninng at ' + http.url );
});
#2
1
Experiment with your code to see what is causing the problem. First try:
试验您的代码,看看导致问题的原因。第一次尝试:
http.listen(4000, '0.0.0.0', function(err) {...
This will solve the problem if it was something to do with your process.env.PORT
variable.
如果它与您的process.env.PORT变量有关,这将解决问题。
If that doesn't work, try:
如果这不起作用,请尝试:
http.listen(4000, function(err) {...
This will solve the problem if it was something to do with the provided hostname (0.0.0.0
). The hostname argument in the listen function is optional, so it should be fine without.
如果它与提供的主机名(0.0.0.0)有关,这将解决问题。 listen函数中的hostname参数是可选的,所以没有它应该没问题。
#3
0
0.0.0.0 is not a valid address to listen to. Try 127.0.0.1 to listen to the loop back on your local machine or get your machine IP address instead.
0.0.0.0不是要侦听的有效地址。尝试使用127.0.0.1在本地计算机上回听环路,或者获取计算机IP地址。
0.0.0.0 means all ports for routing usually.
0.0.0.0表示通常用于路由的所有端口。