我最近一直在研究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!");
});
Run Code Online (Sandbox Code Playgroud)
...现在,虽然我似乎理解代码中发生了什么......我对术语有点困惑....因为当我听到术语服务器时,我会想到像Apache或Nginx这样的东西.我习惯于认为它们就像一个可以容纳我的Web应用程序的容器.Node.js服务器与Nginx/Apache服务器有何不同?基于Node.js的服务器(即代码)仍然可以放在像Nginx这样的东西中运行,这不是真的吗?那么为什么两者都被称为"服务器",尽管Node.js代码似乎是可以使用Nginx放置和提供的应用程序.