node.js - 将expressjs与node-webkit一起使用时的EADDRINUSE

标签 node.js express socket.io node-webkit nw.js

我想使用 nw.js 作为独立的套接字客户端和服务器。我试图将 sample socket.io 聊天应用程序从 socket.io 网站转换为 nw.js 应用程序。但是当我启动 nw.js 时出现以下错误:

[5591:0222/143044:ERROR:nw_shell.cc(325)] Error: listen EADDRINUSE :::3000
    at Object.exports._errnoException (util.js:734:11)
    at exports._exceptionWithHostPort (util.js:757:20)
    at Server._listen2 (net.js:1153:14)
    at listen (net.js:1179:10)
    at Server.listen (net.js:1266:5)
    at Object.<anonymous> (/Users/xxxx/nwjs/chat-example/index.js:23:6)
    at Module._compile (module.js:451:26)
    at Object.Module._extensions..js (module.js:469:10)
    at Module.load (module.js:346:32)
    at Function.Module._load (module.js:301:12)

我尝试了多个端口,但没有一个有效。 这是我用于服务器的代码,与 socket.io 指南提供的代码基本相同。

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
});


io.on('connection', function(socket){
    console.log('a user connected');
    socket.on('disconnect', function(){
        console.log('user disconnected');
    });


    socket.on('chat message', function(msg){
        console.log('message: ' + msg);
        io.emit('chat message', msg);
    });
});

http.listen( 3000, function(){
    console.log('listening on *:3000');
});

这是我的 nw.js 的 package.json

{
  "name": "socket-chat-example",
  "version": "0.0.1",
  "description": "my first socket.io app",
  "dependencies": {
    "express": "^4.10.2",
    "socket.io": "^1.3.4"
  },
  "main": "index.html",
  "node-main": "index.js"
}

我的nwjs版本:v0.12.0-alpha3-osx-x64

最佳答案

查看node-webkit-express一个工作示例。

我无法解释原因,但通过“node-main”启动 Express 是导致问题的原因。也许这是一个多进程问题或其他问题......但你可以做到。

关于node.js - 将expressjs与node-webkit一起使用时的EADDRINUSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28655593/

相关文章:

android - 无法安装 react-native Nativebase : Execution failed for task ':app:compileDebugJavaWithJavac'

node.js - 如何访问 LESS 生成的 AST?

javascript - 使用youtube API和node.js添加youtube评论

javascript - Serverless express 关闭 mongodb 连接

javascript - Node.js Express 发送原始 HTTP 响应

javascript - 从另一个事件 Node js 发出事件

node.js - Node js 中的通用 Socket IO Controller

node.js - 使用 Express Validator 进行模式验证如何设置自定义错误消息?

node.js - 使用 Stylus 和 CoffeeScript 中间件是否会降低 Node.js Express 应用程序的速度?

node.js - io 在客户端未定义