我基本上是想重新创建一个在socket.io官方网站上发布的简单聊天应用程序。
我在content.js中创建了以下模块:
GET /socket.io/?EIO=3&transport=polling&t=1461512892706-14 404 2.370 ms - 1097
我通过以下方式从index.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 passport = require('passport');
var flash = require('connect-flash');
var session = require('express-session');
var routes = require('./routes/index');
var morgan = require('morgan');
var users = require('./routes/users');
var home = require('./routes/home');
var mysql = require('mysql');
var helmet = require('helmet');
var app = express();
// socket.io
var server = require('http').Server(app);
var io = require('socket.io')(server);
require('./config/passport')(passport); // pass passport for configuration
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'mysql',
database: 'clearreview'
});
connection.connect();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
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')));
// security measure against xss attacks
app.use(helmet.xssFilter());
// hide powered by express
app.use(helmet.hidePoweredBy());
// security measure against sniffing
app.use(helmet.noSniff());
app.use('/', routes);
// required for passport
app.use(session({
secret: 'vidyapathaisalwaysrunning',
resave: true,
saveUninitialized: true
})); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// routes ======================================================================
//require('./app/cr-auth-routes.js')(app, passport); // load our routes and pass in our app and fully configured passport
require('./app/cr-auth-routes')(app, passport);
require('./app/search.js')(app, connection);
require('./app/content')(app, io);
// 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;
在运行应用程序时,没有任何内容被发送,并且我的控制台中显示以下错误:
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('clear-review: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);
}
我该如何解决这个问题?据我所知,它与profile.ejs的位置有关?一如既往地感谢您的帮助
这是index.js:
public interface IExample
{
string Name
{
get;
internal set;
}
}
public class Example : IExample
{
private string _name = String.Empty;
string Name
{
get
{
return _name;
}
internal set
{
_name = value;
}
}
}
启动服务器的www文件:
export SWT_GTK3=0
答案 0 :(得分:0)
您似乎正在创建两个单独的服务器,一个位于www
文件中,其中包含:
var server = http.createServer(app);
另一个index.js
中有:
var server = require('http').Server(app);
你永远不会启动在index.js
中创建的那个,而且是socket.io所连接的那个,这就是你没有任何socket.io功能的原因。绑定socket.io的服务器从未启动过。
您正在启动www
文件,但它没有连接socket.io。
您需要创建一个服务器并且只创建一个服务器,并将其作为Express和socket.io绑定的服务器。然后,你需要启动那个服务器。