当我尝试从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 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
从2018-08-18T09_01_29_349Z-debug.log获得以下内容
0 info it worked if it ends with ok
1 verbose cli [ '/opt/plesk/node/9/bin/node',
1 verbose cli '/opt/plesk/node/9/bin/npm',
1 verbose cli 'run',
1 verbose cli 'start' ]
2 info using npm@5.6.0
3 info using node@v9.10.1
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle mongochat@1.0.0~prestart: mongochat@1.0.0
6 info lifecycle mongochat@1.0.0~start: mongochat@1.0.0
7 verbose lifecycle mongochat@1.0.0~start: unsafe-perm in lifecycle true
8 verbose lifecycle mongochat@1.0.0~start: PATH: /opt/plesk/node/9/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/var/www/vhosts/domain.win/console/node_modules/.bin:/opt/plesk/node/9/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
9 verbose lifecycle mongochat@1.0.0~start: CWD: /var/www/vhosts/domain.win/console
10 silly lifecycle mongochat@1.0.0~start: Args: [ '-c', 'node server.js' ]
11 silly lifecycle mongochat@1.0.0~start: Returned: code: 1 signal: null
12 info lifecycle mongochat@1.0.0~start: Failed to exec start script
13 verbose stack Error: mongochat@1.0.0 start: `node server.js`
13 verbose stack Exit status 1
13 verbose stack at EventEmitter.<anonymous> (/opt/plesk/node/9/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:285:16)
13 verbose stack at EventEmitter.emit (events.js:180:13)
13 verbose stack at ChildProcess.<anonymous> (/opt/plesk/node/9/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack at ChildProcess.emit (events.js:180:13)
13 verbose stack at maybeClose (internal/child_process.js:936:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:220:5)
14 verbose pkgid mongochat@1.0.0
15 verbose cwd /var/www/vhosts/domain.win/console
16 verbose Linux 3.10.0-862.3.3.el7.x86_64
17 verbose argv "/opt/plesk/node/9/bin/node" "/opt/plesk/node/9/bin/npm" "run" "start"
18 verbose node v9.10.1
19 verbose npm v5.6.0
20 error code ELIFECYCLE
21 error errno 1
22 error mongochat@1.0.0 start: `node server.js`
22 error Exit status 1
23 error Failed at the mongochat@1.0.0 start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]
这是简单的server.js
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');
});
});
});
});
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"
}
}
答案 0 :(得分:0)
端口4000上可能已在运行另一台服务器。尝试使用其他端口或关闭已在运行的服务器。