Search code examples
htmlnode.jsserverside-javascriptserver-side-scripting

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 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?


Solution

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