http.createServer() & express.createServer()有区别吗?
发布于 1年前 作者 wanghantong 2522 次浏览

在socket.io的简介中看到的关于server的创建方式

var app = require(‘express’)(); var server = require(‘http’).createServer(app); ***************some setting… server.listen(port);


var app = require(‘express’).createServer(); ********************any setting??? app.listen(port);


1.这两种创建方式有什么区别吗?还是一样的效果?

2.对于第一种方式使用时,可以进行配置如app.set(port)、app.set(“view”)、app.set(“view engine”)、 app.use(express.logger(‘dev’)); app.use(express.bodyParser());…想请教, 是不是这些配置在 server.listen()之前配置就会都生效???? 3.在第二种方式创建server的时候,也使用上述配置,配置还可否生效呢?

4.在引入socket.io模块的时候,要监听server,那么我正确的引入socket.io是要在什么时候呢?是在那些配置之后呢,还是在配置之前就可以???还是socket.io只是在监听该server,和web操作的这些配置没有任何关系呢?此处求详解!!!

var app = require(‘express’)(); var server = require(‘http’).createServer(app);

var io = require(‘socket.io’).listen(server);//这个位置引入 ***************some setting… var io = require(‘socket.io’).listen(server);//和上面引入有区别吗?

server.listen(port);

var app = require(‘express’).createServer();

var io = require(‘socket.io’).listen(server);//这个位置引入 ********************any setting??? var io = require(‘socket.io’).listen(app);//和上面引入有区别吗?

app.listen(port);


4 回复
var express = require('express'),
    path = require('path'),
    http = require('http'),
    io = require('socket.io'),
    wine = require('./routes/wines');

var app = express();

app.configure(function () {
    app.set('port', process.env.PORT || 3000);
    app.use(express.logger('dev'));
    app.use(express.bodyParser())
    app.use(express.static(path.join(__dirname, 'public')));
});

var server = http.createServer(app);
io = io.listen(server);


io.configure(function () {
    io.set('authorization', function (handshakeData, callback) {
        if (handshakeData.xdomain) {
            callback('Cross-domain connections are not allowed');
        } else {
            callback(null, true);
        }
    });
});

server.listen(app.get('port'), function () {
    console.log("Express server listening on port " + app.get('port'));
});

app.get('/wines', wine.findAll);
app.get('/wines/:id', wine.findById);
app.post('/wines', wine.addWine);
app.put('/wines/:id', wine.updateWine);
app.delete('/wines/:id', wine.deleteWine);

io.sockets.on('connection', function (socket) {

    socket.on('message', function (message) {
        console.log("Got message: " + message);
        ip = socket.handshake.address.address;
        url = message;
        io.sockets.emit('pageview', { 'connections': Object.keys(io.connected).length, 'ip': '***.***.***.' + ip.substring(ip.lastIndexOf('.') + 1), 'url': url, 'xdomain': socket.handshake.xdomain, 'timestamp': new Date()});
    });

    socket.on('disconnect', function () {
        console.log("Socket disconnected");
        io.sockets.emit('pageview', { 'connections': Object.keys(io.connected).length});
    });

});

这是一个例子,我没细看,你可以参考

exports.createServer = function(){
  console.warn('Warning: express.createServer() is deprecated, express');
  console.warn('applications no longer inherit from http.Server,');
  console.warn('please use:');
  console.warn('');
  console.warn('  var express = require("express");');
  console.warn('  var app = express();');
  console.warn('');
  return createApplication();
};

[地址][https://github.com/visionmedia/express/blob/master/lib/express.js]

源码里的这个函数,说明第二种写法是过时了的…应该用第一种写法

哥,咋看出来的?我没看懂?上面这段代码能说明什么?

@wanghantong 那几行警告不是写了吗,express.createServer()是过时的方法

回到顶部