Skip to content Skip to sidebar Skip to footer

Call Nodejs Scripts From Html Pages

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 us

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

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

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

Solution 4:

You could use CGI. Something like this:

#!/usr/local/bin/nodevar 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/../..

Post a Comment for "Call Nodejs Scripts From Html Pages"