我的快递服务器说端口80和443正在使用,但`netstat -nlp`则表示不然

时间:2015-04-17 05:20:01

标签: linux node.js ubuntu express netstat

我尝试在端口80和443上运行我的Express服务器,但是我收到的错误是它们已被使用。

EADDRINUSE err

但是,我的Ubuntu服务器说实际上端口80和443并不忙:

ubuntu@ip-182-47-78-432:~$ sudo netstat -nlp

我明白了:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      4549/node
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1102/sshd
tcp        0      0 0.0.0.0:27000           0.0.0.0:*               LISTEN      14651/mongod
tcp        0      0 0.0.0.0:8090            0.0.0.0:*               LISTEN      4549/node
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      12374/mongod
tcp6       0      0 :::22                   :::*                    LISTEN      1102/sshd
udp        0      0 0.0.0.0:68              0.0.0.0:*                           580/dhclient
udp        0      0 0.0.0.0:23728           0.0.0.0:*                           580/dhclient
udp6       0      0 :::1742                 :::*                                580/dhclient
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name    Path
unix  2      [ ACC ]     STREAM     LISTENING     8966     1104/acpid          /var/run/acpid.socket
unix  2      [ ACC ]     SEQPACKET  LISTENING     7449     412/systemd-udevd   /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     8521     869/dbus-daemon     /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     7430879  14651/mongod        /tmp/mongodb-27000.sock
unix  2      [ ACC ]     STREAM     LISTENING     1636971  1/init              @/com/ubuntu/upstart
unix  2      [ ACC ]     STREAM     LISTENING     19618017 20335/node          /home/ubuntu/.forever/sock/worker.1429234248700JdV.sock
unix  2      [ ACC ]     STREAM     LISTENING     8616395  12374/mongod        /tmp/mongodb-27017.sock
unix  2      [ ACC ]     STREAM     LISTENING     19620699 21172/node          /home/ubuntu/.forever/sock/worker.1429234407027ZZT.sock

会发生什么?或者我可以运行其他命令来找到有用的东西?


编辑1:

我的代码:

var fs = require('fs');
var http = require('http');
var https = require('https');
var app = require('./app');

var credentials = {
    key: fs.readFileSync('private_key.pem', 'utf8'),
    cert: fs.readFileSync('com_certificate.pem', 'utf8'),
    ca: [
        fs.readFileSync('com_certificate_chain_1.pem', 'utf8'),
        fs.readFileSync('com_certificate_chain_2.pem', 'utf8')
    ]
};

http.createServer(app).listen(80, function() {
    console.log('HTTP server started on port ' + 80 + '...');   
});

https.createServer(credentials, app).listen(443, function() {
    console.log('HTTPS server started on port ' + 443 + '...');
});

编辑2:

我在亚马逊云服务器上,没有运行Skype。

编辑3:

var express = require('express');
var app = module.exports = express();
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var methodOverride = require('method-override');
var cors = require('cors');
var corsOptions = require('./config/cors');
var config = require('./config/db');
var spreadsheet = require('./services/spreadsheet');

// models
var User = require('./app/models/user.js');
var Goal = require('./app/models/goal.js');
var Portfolio = require('./app/models/portfolio.js');
var Security = require('./app/models/security.js');

// environment variables
app.set('views', __dirname + '/app/views');
app.set('view engine', 'ejs');

// configure db
app.set('/api/v1', config.db[app.settings.env]);
mongoose.connect(app.get('/api/v1'), function(err) {
    if(err) { return console.error(err); }
});

// mongoose.connection.on('error', console.error.bind(console, 'Connection error: '));
mongoose.connection.once('open', function cb() {
    console.log('Connected.');
    console.log(app.settings.env);
});

// get data from a POST request
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// get data from cookies
app.use(cookieParser());

// make PUT and DELETE requests from views
app.use(methodOverride(function(req, res) {
    if (req.body && typeof req.body === 'object' && '_method' in req.body) {
        var method = req.body._method;
        delete req.body._method;
        return method;
    }
}));

// enable pre-flight and cors for every route
app.options('*', cors());
app.use(cors(corsOptions));

// force HTTPS redirect
app.use(function(req, res, next) {
    if (!req.secure) {
        return res.redirect('https://' + req.get('host') + req.url);
    }
    next();
});

// api routes
var user_routes = require('./routes/user_routes');
var security_routes = require('./routes/security_routes');
var portfolio_routes = require('./routes/portfolio_routes');
var goal_routes = require('./routes/goal_routes');
app.use('/api/v1/users', user_routes);
app.use('/api/v1/securities', security_routes);
app.use('/api/v1/portfolios', portfolio_routes);
app.use('/api/v1/goals', goal_routes);

// MVC routes
var user_controller = require('./app/controllers/user_controller');
var security_controller = require('./app/controllers/security_controller');
var portfolio_controller = require('./app/controllers/portfolio_controller');
var goal_controller = require('./app/controllers/goal_controller');
app.use('/users', user_controller);
app.use('/securities', security_controller);
app.use('/portfolios', portfolio_controller);
app.use('/goals', goal_controller);

编辑4:

尝试运行服务器时收到的错误消息:

ubuntu@ip-172-31-15-213:~/services/api$ nodemon server.js
17 Apr 21:12:17 - [nodemon] v1.3.7
17 Apr 21:12:17 - [nodemon] to restart at any time, enter `rs`
17 Apr 21:12:17 - [nodemon] watching: *.*
17 Apr 21:12:17 - [nodemon] starting `node server.js`

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EACCES
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1023:19)
    at listen (net.js:1064:10)
    at Server.listen (net.js:1138:5)
    at Object.<anonymous> (/home/ubuntu/services/api/server.js:17:24)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
17 Apr 21:12:17 - [nodemon] app crashed - waiting for file changes before starting...

这次看起来我有一个不同的错误......

编辑5:

如果我运行telnet localhost 80,我会:

ubuntu@ip-172-31-15-213:~/services/api$ telnet localhost 80
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

2 个答案:

答案 0 :(得分:1)

您的错误是Error: listen EACCES,而不是EADDRINUSE

EACCES的原因是绑定到1024以下的端口(如80和443)需要root权限,而你似乎没有以root身份启动你的应用程序(在此之前,请确保你知道以root用户身份运行应用程序的含义。

答案 1 :(得分:-1)

如果您使用的是Skype

尝试禁用它以使用端口80进行连接

Open Skype if it has not already been launched
Go to Tools –> Options from the dropdown menu
Select “Advanced” in the left-hand column, last option
Select “Connection”
Deselect the option that says, “Use port 80 and 443 as alternatives for incoming     connections”
Click Save
Exit and then restart Skype

here是链接