从html页面调用nodejs脚本

发布于 2024-11-25 18:54:23 字数 296 浏览 7 评论 0原文

我想像调用 php 一样调用 node.js 脚本,只需使用它们的 url 即可。

我主要是一名 js 程序员,所以如果我完全放弃 php 并使用 Node 来进行服务器端脚本编写,那就太棒了。

但到目前为止我看到的大部分教程都涉及创建服务器等...

我只想以旧方式调用脚本,例如 www.mysite.com/login.js 而不是 www.mysite.com/login。 。

一旦调用,脚本应该返回一个用于渲染的页面,或者简单地返回 json 或其他用于 ajax 调用的文本

是否可以?

I would like to call node.js scripts like i do with php, that is simply using their url.

I'm mainly a js programmer, so it would be wonderful for me to ditch out php totally and use node for the server side scripting.

But most of the tutorial i saw until now involved creating a server, etc...

I just want to call a script in the old way, like www.mysite.com/login.js instead of www.mysite.com/login.php

And once called the script should return or a page for rendering or simply json or other text for ajax calls.

Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我一向站在原地 2024-12-02 18:54:23

还有另一种可能性,类似于上面提到的 CGI 方法,但在 Node.js 中使用支持 FastCGI 的模块(https://github.com/samcday/node-fastcgi-application),这将允许 Apache 与之对话。

它实际上混合了两种风格,因此 Node 程序由 Apache 自动启动,但只要有请求需要处理,它就会一直存在。您只需设置一条规则,将所需的页面重定向到dispatch.njs 脚本,该脚本已使用.htaccess 中的AddType 作为节点脚本添加,该脚本启动并处理stdin 上的请求,并将结果发送到stdout。但你仍然需要express提供的路由,因为它只查看HTTP_REQUEST_URI来确定你想要哪个页面。

另一种选择是设置 Node 侦听某个端口,并在 Apache 与某个签名匹配(例如以 .njs 结尾)时代理对它的请求。

There's another possibility, similar to the CGI method mentioned above but using a module (https://github.com/samcday/node-fastcgi-application) in Node.js that supports FastCGI which would allow Apache to talk to it.

It actually blends both styles, so that the Node program is launched automatically by Apache but stays around as long as there are requests to process. You simply set up a rule to redirect the pages you want to a dispatch.njs script, which you have added with AddType in .htaccess as a Node script, which launches and then handles requests on the stdin and sends the results to stdout. But you still need the routing provided by express because it's only looking at HTTP_REQUEST_URI to determine what page you want.

Another option would be to setup Node listening on a certain port and proxy requests to it from Apache if they match a certain signature (like ends in .njs).

梦巷 2024-12-02 18:54:23

但到目前为止我看到的大部分教程都涉及创建服务器等......

我只想以旧方式调用脚本,例如 www.mysite.com/login.js 而不是 www.mysite.com/login.php

这就是node.js 的工作原理。您创建一个服务器。对于 PHP,教程的第一步是安装和设置 apache(创建服务器)。

用 PHP 术语来说,你的问题的等价物是

我可以在不安装 apache/nginx/其他网络服务器的情况下运行 PHP 脚本

?你不能(我相信最近或未来的版本包括内置的 Web 服务器,就像 Node.js 一样!)

你需要安装 Node.js,你需要告诉节点运行 Web 服务器

但是,您可以使用 expressjs 进行更简化和熟悉的设置。然后,您只需在命令行上调用 express 即可搭建您的服务器。

你仍然需要安装node.js(和npm)

But most of the tutorial i saw until now involved creating a server, etc...

I just want to call a script in the old way, like www.mysite.com/login.js instead of www.mysite.com/login.php

That's how node.js works. You create a server. With PHP the first step of a tutorial is to install and setup apache (creating the server).

The equivelant of your question in PHP terms would be

Can I run PHP scripts without installing apache/nginx/other webserver

Which you can't (I believe recent or future version include a web server baked in, just like node.js !)

You need to install node.js, you need to tell node to run a web server

However you can use expressjs for a more streamlined and familiar setup. You can then just call express on the command line to scaffold your server out.

You still have to install node.js (and npm)

丑疤怪 2024-12-02 18:54:23

Node.js 和 PHP 是两个不同的东西。

Node.js 是一个“事件驱动的 I/O 服务器端 JavaScript 环境”。当它运行时,Javascript 并不是作为脚本语言运行,而是像 Ruby 或 Python 一样进行处理。您启动服务器,然后运行代码。

然而,PHP 作为脚本语言在 Web 服务器上运行,因为 Web 服务器上安装了 PHP 处理器模块。因此,您可以直接通过 .php 扩展名运行 PHP 脚本,因为 Apache 服务器被配置为将 .php 文件解释为脚本。

换句话说,如果没有大量的技巧(使用 Node.js),您想要做的事情是不可能实现的。

但是,如果您只想使用 JavaScript 而不是 PHP,我会查看 JS-CGI,它允许您使用Javascript作为CGI扩展。

Node.js and PHP are two different things.

Node.js is an "event-driven I/O server-side JavaScript environment". When it functions, Javascript is not run as a scripting language, it is processed just like Ruby or Python. You start a server, and the code is run.

PHP, however, is run as a scripting language on a webserver, because the web server has a PHP processor module installed on it. Therefore, you can run PHP scripts directly by the .php extension, because the Apache server is configured to interpret .php files as scripts.

In other words, what you'd like to do is not possible without a large amount of hacky tricks, with node.js.

However, if you'd like to just use JavaScript instead of PHP, I'd check out JS-CGI, which allows you to use Javascript as a CGI extension.

药祭#氼 2024-12-02 18:54:23

你可以使用CGI。像这样的东西:

#!/usr/local/bin/node

var sys=require("sys");
sys.puts("Content-type: text/html\n");
sys.puts("Hello World!<br/>\n");
var argstr="";
for(var i in process.env){
  argstr+=i+": " + process.env[i] + "<br/>\n";
}
sys.puts("args: "+ argstr +"<br/>\n");

就像 Perl/Python/../..

You could use CGI. Something like this:

#!/usr/local/bin/node

var sys=require("sys");
sys.puts("Content-type: text/html\n");
sys.puts("Hello World!<br/>\n");
var argstr="";
for(var i in process.env){
  argstr+=i+": " + process.env[i] + "<br/>\n";
}
sys.puts("args: "+ argstr +"<br/>\n");

Just like Perl/Python/../..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文