问题已解决。
请参阅下面的答案。
问题:
我正试图在相同的端口上进行socketio并表达工作,这是我得到的错误。
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object.exports._errnoException (util.js:874:11)
at exports._exceptionWithHostPort (util.js:897:20)
at Server._listen2 (net.js:1221:19)
at listen (net.js:1270:10)
at Server.listen (net.js:1366:5)
at Object.<anonymous> (/Users/app.js:22:8)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
我可以使用sudo npm start启动我的服务器,但现在我有两台运行的服务器,localhost:3000(express)和localhost:80(express和socket.io)
如何在不破坏任何内容的情况下禁用:3000 箱/万维网?
我正在使用Express 4。
这是我的观点
html
head
title Socket.io
script(src="/socket.io/socket.io.js")
body
script(src="javascripts/ol.js")
ol.js(客户端)
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', {my: 'data'});
});
这是我的app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var http = require('http');
var router = express.Router();
var app = express();
var server = http.Server(app);
var io = require('socket.io')(server);
server.listen(80);
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
io.on('error', function () {
console.log("errr");
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.set('view options', {layout: false});
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/testit', function (req, res) {
res.send("it works!");
});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
当我使用3000而不是80用于server.listen并且如果我使用节点app.js而不是npm开始运行我的应用程序,我没有看到任何问题,一切正常,但socketio覆盖Express.js的设置那样的?
我根本没碰到bin / www文件。就在下面。
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('future:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
答案 0 :(得分:4)
io.js
var io = require('socket.io')(); // yes, no server arg here; it's not required
// attach stuff to io
module.exports = io;
在var app = require('../app');
var io = require('../io');
在io.attach(server);
server.listen(port)
在io.on
var app = express();
部分
var io = require('./io');
io.on('connection', function (socket) {
socket.emit('news', {hello: 'world'});
socket.on('my other event', function (data) {
console.log(data);
});
});
io.on('error', function () {
console.log("errr");
});
最后,我们的观点。不要担心socket.io.js的存在,它就在那里。您不必手动下载它或包含除此之外的一些外部文件。
html
head
title Socket.io
script(src="/socket.io/socket.io.js")
body
script(src="javascripts/ol.js")
ol.js(测试)
var socket = io.connect('http://localhost:3000');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', {
my: 'data'
});
});