从html页面调用nodejs脚本 [英] call nodejs scripts from html pages

查看:2700
本文介绍了从html页面调用nodejs脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用node.js脚本,就像我用php做的那样,就是简单地使用它们的url。



我主要是一个js程序员,所以它对我来说完全不用PHP,并且使用节点来进行服务器端脚本。



但是我直到现在看到的大部分教程都涉及到创建服务器等。 ..



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



并且一旦调用该脚本应该返回或者一个页面进行渲染,或者简单地用json或其他文本进行ajax调用。



<有可能吗?

解决方案

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



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



另一种选择是设置Node监听某个端口,如果它们匹配某个签名(如以.njs结尾),那么它将从Apache发出代理请求。


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?

解决方案

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).

这篇关于从html页面调用nodejs脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆