gpt4 book ai didi

node.js - Nodejs/Express - 为什么 http.createServer(app) 内部需要应用程序?

转载 作者:太空宇宙 更新时间:2023-11-03 23:21:35 27 4
gpt4 key购买 nike

// get dependencies
const express = require("express");
const http = require('http');
const app = express();

const server = http.createServer(app);

为什么http.createServer(app)内部需要app?它和 http.createServer() 有什么区别?

我已经阅读了文档,但还是不明白。谁能解释一下吗?

最佳答案

之所以将app传递给http.createServer,与Dependency Injection有关。 ,一种定义外部依赖关系的模式。

http.createServer 接受将在内部使用的外部类来处理传入消息和传出响应。

这允许您编写自己的类来处理传入/传出消息,而无需自己修改 http 的内部结构。在您的例子中,您正在使用 Express 的类。

关于node.js - Nodejs/Express - 为什么 http.createServer(app) 内部需要应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49105774/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com