【问题标题】:express and http server usage togatherexpress 和 http 服务器的使用一起
【发布时间】:2019-02-04 08:00:40
【问题描述】:

通过下面的代码无法理解代码1的工作......下面两个代码有什么区别

**

在代码1中同时使用http和express有什么意义?

代码1

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

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

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

可以做同样的事情

代码2

var express=require('express');

var app=express();

var socket=require('socket.io');

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

}).listen(8080);
console.log("Listening to port 8080");

【问题讨论】:

    标签: node.js http express socket.io


    【解决方案1】:

    你问的是expressjs自己的服务器和http服务器的区别。它们在很多方面都不同。

    Solved here

    【讨论】:

      【解决方案2】:

      app 对象通常表示 Express 应用程序,它由 Express 模块导出的顶级 express() 函数创建。

      http.listen(): 启动 HTTP 服务器监听连接

      在第二种情况下,app.listen() 绑定并监听指定端口上的连接,它与http.listen() 相同

      【讨论】:

        猜你喜欢
        • 2018-09-15
        • 2017-03-18
        • 1970-01-01
        • 2017-05-26
        • 2017-07-01
        • 2016-08-06
        • 2012-04-15
        • 1970-01-01
        • 2011-10-22
        相关资源
        最近更新 更多