var httpsport = 8080, // used for httpsapp
httpport = 8000, // used for httpapp
numCPUs = require('os').cpus().length;
var credentials = {
key: fs.readFileSync('./cert/client.key'),
cert: fs.readFileSync('./cert/client.crt'),
requestCert: true
};
var cluster = require('cluster'),
socketStore = require('socket.io-clusterhub');
var redis = require('redis');
var redisClient = redis.createClient();
if(cluster.isMaster) {
for(var i=0; i<numCPUs; i++) {
cluster.fork();
}
} else {
var io = require('socket.io'),
express = require('express'),
httpsapp = express(), // for https services
httpapp = express(), // for http services
http = require('http'),
https = require('https');
httpapp.configure( function () {
httpapp.use(express.bodyParser());
httpapp.use(express.methodOverride);
httpapp.use(httpapp.router);
httpapp.set('port', httpport);
httpapp.use(express.static(__dirname+'/public', {maxAge:oneDay}));
});
httpsapp.configure( function() {
// allow CORS
httpsapp.all('*', function(req, res, next){
if(!req.get('Origin')) {
return next();
}
// use "*" here to accept any origin
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Methods', 'GET');
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
// res.set('Access-Control-Allow-Max-Age', 3600);
if ('OPTIONS' === req.method) {
return res.send(200);
}
next();
});
httpsapp.use(express.bodyParser());
httpsapp.use(express.static(__dirname + '/public'));
httpsapp.set('port', httpsport);
});
var httpServer = http.createServer(httpapp),
httpsServer = https.createServer(credentials, httpsapp),
sio = io.listen(httpServer);
// set the configuration for production.
sio.configure('production', function(){
sio.enable('browser client etag');
sio.set('log level', 1);
sio.set('store',socketStore);
sio.set('transports', [
'websocket',
'flashsocket',
'htmlfile',
'xhr-polling',
'jsonp-polling'
]);
});
sio.of('/namespace1').on('connection', function(socket) {
socket.on('doSomething', function() {
socket.emit('reply',{hello:world}) });
httpsapp.get("/user", function(req, res) {
// ...
});
}
}
如果使用节点集群,我会收到响应:Cannot GET /user。但是没有集群,它能够为 https.get("/user") 提供服务。
另外,使用集群,我想检查redis、http(s)、socket.io和express模块是应该在worker部分声明还是全局声明?
httpsapp.get() 嵌套在套接字空间中,因为它想要回复特定的套接字。有没有办法绕过这个结构?
任何人都可以弄清楚我们为什么 httpsapp.get() 不为请求提供服务?还有这些声明在哪里合适?谢谢!