是否可以 运行 javascript 作为 Apache 上的 cgi 脚本?

Is it possible to run javascript as cgi script on Apache?

我设法将 Apache 配置为 运行 cgi 脚本,并尝试使用 javascript 生成输出。

#!C:\php\php.exe

<script>
  document.write("Hello world!");
</script>

然而,这只发送内容,document.write 在客户端是 运行。我想通过 javascript.

发送 Hello world!

我知道有nodejs。我想知道是否有 Apache 的模块或某些设置可以在服务器上启用 运行ning javascript 并仅发送 document.write?

的内容

CGI 显式生成一个外部进程,因此会使用 mod_cgi。然后你可以生成一个 Node.js 进程。

但是,您需要编写一个真正的 CGI 程序,而不是使用依赖于浏览器 API 的嵌入式 JS 的 HTML 片段。

#!/usr/bin/env node

process.stdout.write("Content-Type: text/plain\r\n\r\n");
process.stdout.write("Hello, world");

CGI 效率不高,Node 是一种奇怪的语言选择。如果您需要通过 Apache,那么 运行 使用 Node 的服务器然后使用 mod_proxy 通过 Apache 访问它通常更有意义。