call nodejs scripts from html pages

耗尽温柔 提交于 2019-12-11 17:37:39

问题


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?


回答1:


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




回答2:


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)




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/6807388/call-nodejs-scripts-from-html-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!