最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

使用net.server来处理http并立即表达所有内容

运维笔记admin12浏览0评论

使用net.server来处理http并立即表达所有内容

使用net.server来处理http并立即表达所有内容

我有一台服务器使用net.Server,还有2台服务器使用http.Serverexpress()。它看起来像这样

...
var s_net = net.createServer();
var s_http = http.createServer();
var s_exp = express();
s_net.listen(opt.port);
s_http.listen(s_net);
s_exp.listen(s_net);
console.log(`listening on ${opt.port}`);

我使用s_net作为s_https_exp的处理程序。然后,我做

s_net.on("connection", function(c){
    console.log("incoming connection to s_net")
})
s_http.on("request", function(req, res){
    console.log("request to s_http");
});
s_exp.all("/*", function(req, res, next){
    console.log("request to s_exp");
    res.send("<h1>Hello World!</h1>");
});

但是在控制台上,我得到了

H:\NodeJS\web3>node index.js
listening on 80
request to s_exp

在哪里,它只响应s_exp请求。 我怎么能得到它们? incoming connection to s_netrequest to s_httprequest to s_exp 参考: .x/docs/api/net.html#net_server_listen_handle_backlog_callback

回答如下:

使用Express创建服务器时,您已经拥有了http服务器。它只是一个http服务器,它具有Express作为传入请求的请求处理程序。并且,http服务器继承自net.Server,因此它已经是TCP / IP服务器。创建一个快速服务器,然后您可以使用任何express,http或net.Server功能,包括为各种服务器事件设置事件侦听器。

因此,只需创建一个通用的Express服务器就已经将这三个服没有必要或指向创建三个单独的服务器,然后尝试以某种方式组合它们。

发布评论

评论列表(0)

  1. 暂无评论