gpt4 book ai didi

linux - 我的 Express 服务器说端口 80 和 443 正在使用,但 `netstat -nlp` 却说相反

转载 作者:太空宇宙 更新时间:2023-11-04 03:40:51 25 4
gpt4 key购买 nike

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

EADDRINUSE 错误

但是,我的 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:

我在 Amazon 云服务器上,它没有运行 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

最佳答案

您的错误显示错误:监听EACCES,而不是EADDRINUSE

EACCES 的原因是绑定(bind)到 1024 以下的端口(例如 80 和 443)需要 root 权限,而您似乎并未以 root 身份启动应用程序(在此之前,请确保您了解以 root 用户身份运行应用程序的影响)。

关于linux - 我的 Express 服务器说端口 80 和 443 正在使用,但 `netstat -nlp` 却说相反,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29690953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com