【发布时间】:2023-03-08 15:30:01
【问题描述】:
我最近一直在研究 Node.js,并且偶然发现了一些关于编写基于 Node.js 的简单服务器的资料。比如下面的。
var express = require("express"),
http = require("http"), app;
// Create our Express-powered HTTP server
// and have it listen on port 3000
app = express();
http.createServer(app).listen(3000);
// set up our routes
app.get("/hello", function (req, res) {
res.send("Hello World!");
});
app.get("/goodbye", function (req, res) {
res.send("Goodbye World!");
});
现在,虽然我似乎理解了代码中发生了什么,但我对术语感到有些困惑。当我听到服务器一词时,我会想到 Apache 或 Nginx 之类的东西。我习惯于将它们视为可以容纳我的 Web 应用程序的容器。 Node.js 服务器与 Nginx/Apache 服务器有何不同?基于 Node.js 的服务器(即代码)是否仍然可以放置在 Nginx 之类的东西中运行?那为什么都叫“服务器”呢?
【问题讨论】:
-
Isn't it true that a Node.js based server (i.e. code) will still be placed within something like Nginx to run?不,不正确 -
从技术上讲,您可以运行您的应用程序并让节点将其提供给客户端,从而有效地充当网络服务器的角色,但您可能比您想要咀嚼的更多。我最近阅读了这篇关于该主题的精彩文章:nginx.com/blog/nginx-vs-apache-our-view
-
让我澄清一下,当我询问两者之间的区别时,我并没有考虑 apache 和 nginx。我正在讨论 Node.js 与 Nginx。
-
不要让标题误导你。如果你读了这篇文章,你就会明白为什么我说虽然你可以自己做,但你可能不想......
标签: javascript node.js apache nginx webserver