Node.js 无法在nodejs上启动服务器
当我试图从plesk node命令运行Node.js 无法在nodejs上启动服务器,node.js,express,plesk-onyx,Node.js,Express,Plesk Onyx,当我试图从plesk node命令运行node server.js时,我从命令中得到一个错误 我从命令中得到以下错误: > mongochat@1.0.0 start /var/www/vhosts/domain.win/console > node server.js events.js:165 throw er; // Unhandled 'error' event ^ Error: listen EADDRINUSE :::4000 at S
node server.js
时,我从命令中得到一个错误
我从命令中得到以下错误:
> mongochat@1.0.0 start /var/www/vhosts/domain.win/console
> node server.js
events.js:165
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::4000
at Server.setupListenHandle [as _listen2] (net.js:1345:14)
at listenInCluster (net.js:1386:12)
at Server.listen (net.js:1474:7)
at Server.listen.Server.attach (/var/www/vhosts/domain.win/console/node_modules/socket.io/lib/index.js:273:9)
at new Server (/var/www/vhosts/domain.win/console/node_modules/socket.io/lib/index.js:59:17)
at Function.Server [as listen] (/var/www/vhosts/domain.win/console/node_modules/socket.io/lib/index.js:44:41)
at Object.<anonymous> (/var/www/vhosts/domain.win/console/server.js:2:37)
at Module._compile (module.js:649:30)
at Object.Module._extensions..js (module.js:660:10)
at Module.load (module.js:561:32)
Emitted 'error' event at:
at emitErrorNT (net.js:1365:8)
at process._tickCallback (internal/process/next_tick.js:114:19)
at Function.Module.runMain (module.js:692:11)
at startup (bootstrap_node.js:194:16)
at bootstrap_node.js:666:3
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! mongochat@1.0.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the mongochat@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /var/www/vhosts/domain.win/.npm/_logs/2018-08-18T09_01_29_349Z-debug.log
package.json
{
"name": "mongochat",
"version": "1.0.0",
"description": "Simple chat app using sockets",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"mongodb": "^2.2.30",
"socket.io": "^2.0.3"
}
}
可能另一台服务器已在端口4000上运行。请尝试另一个端口或关闭已经运行的服务器。我如何才能做到这一点?我基本上不熟悉第2行server.js中的节点serverIn,将4000修改为,例如4001.js。问题是,当我切换到新端口,然后运行
start
时,它会在加载超过一小时后执行命令。因此,如果我在加载过程中关闭命令,并再次使用相同的端口运行start
,它就会触发错误,并说error:listen EADDRINUSE:::4001
您确定要关闭旧服务器(例如在命令行中使用Ctrl+C)?如果在端口4001上启动节点服务器,然后在端口4001上再次运行节点服务器,则错误listen EADDRINUSE:::4001
可以。两台服务器不能同时在同一端口上运行。您是否在控制台上看到消息“MongoDB connected…”?
const mongo = require('mongodb').MongoClient;
const client = require('socket.io').listen(4000).sockets;
// Connect to mongo
mongo.connect('mongodb://***:***@ds121382.mlab.com:21382/db', function(err, db){
if(err){
throw err;
}
console.log('MongoDB connected...');
// Connect to Socket.io
client.on('connection', function(socket){
let chat = db.collection('chats');
// Create function to send status
sendStatus = function(s){
socket.emit('status', s);
}
// Get chats from mongo collection
chat.find().limit(100).sort({_id:1}).toArray(function(err, res){
if(err){
throw err;
}
// Emit the messages
socket.emit('output', res);
});
// Handle input events
socket.on('input', function(data){
let name = data.name;
let message = data.message;
// Check for name and message
if(name == '' || message == ''){
// Send error status
sendStatus('Please enter a name and message');
} else {
// Insert message
chat.insert({name: name, message: message}, function(){
client.emit('output', [data]);
// Send status object
sendStatus({
message: 'Message sent',
clear: true
});
});
}
});
// Handle clear
socket.on('clear', function(data){
// Remove all chats from collection
chat.remove({}, function(){
// Emit cleared
socket.emit('cleared');
});
});
});
});
{
"name": "mongochat",
"version": "1.0.0",
"description": "Simple chat app using sockets",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"mongodb": "^2.2.30",
"socket.io": "^2.0.3"
}
}