gpt4 book ai didi

javascript - node.js 实现背后的基本思想?

转载 作者:行者123 更新时间:2023-11-30 18:06:58 26 4
gpt4 key购买 nike

这是我目前关于 node.js 将如何工作的理论:

  1. 我在我的服务器上安装了 node.js,以便它可以解释 javascript 文件。
  2. 然后我编写我的 javascript 文件并将它们放在我的服务器上(就像我处理 PHP 文件一样)。
  3. 然后我通过我的客户端 javascript 与这些文件进行交互。

显然我出了点问题,因为过去一个小时左右我一直在寻找一个教程,它会教我如何在我的服务器上安装它 - 但他们似乎都专注于在本地安装它。

谁能简要介绍一下最终实现的工作原理?

最佳答案

您可以像在任何其他机器上一样将它安装在“服务器”上——通过 installer 获得管理员/root 访问权限。或 package manager .

现在,这里假定“服务器”指的是一台计算机。相反,如果您指的是现有的“服务器应用程序”,例如 Apache 或 IIS——Node.js 不会直接与这些应用程序集成。它主要取代了它们,允许您从相当低的级别将整个服务器应用程序定义为脚本。

这样的脚本可以在 project's homepage 上找到:

This simple web server written in Node responds with "Hello World" for every request.

var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

To run the server, put the code into a file example.js and execute it with the node program from the command line:

% node example.js
Server running at http://127.0.0.1:1337/

除此示异常(exception),您还需要检查 req.methodreq.url , 通常通过 routerweb framework , 决定如何回应。 expresscompoundjs将是不错的选择。

您仍然可以使用其他服务器应用程序作为 Node.js 的 HTTP 代理,传递流量。但 Node.js 仍将单独运行。如果您使用的是 IIS,甚至还有 iisnode其中涵盖了大部分设置。

关于javascript - node.js 实现背后的基本思想?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15587337/

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