call nodejs scripts from html pages - html

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

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)

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.

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

Related

How to run a python script when a hyperlink is clicked from a webpage?

I have some code on my mac in the latest version of python idle 3, that collects certain data from a csv file that gets sent to myself and prints out the output in the terminal. I want to create a webpage that has a button or link that a user clicks and it runs the code and prints out the output of my program.
Eventually i want to be able to create a website with multiple links that can do the same operation.
Will i need to create an sql database? If so how?...
From the sound of it, you want to use a webpage as a user interface for your python script. Unfortunately without utilising a server-side language this is not possible.
Multiple options exist for reacting to a button press on the server side, with PHP being the most well known, but solutions using only python do exist, such as Flask.
If you're just after a local GUI for your script, simpler options exist within python such as Tk.
Actually you can expose this function using a webserver and then the webpage will call the server with the right url.
Since you are using python I will recommend to take a look at Flask http://flask.pocoo.org/ great micro framework to get you started.

Call a node.js function inside an html file

This is most likely a repeated question.
The closest I got to an answer was here: execute a Nodejs script from an html page?
Yet, I still can't understand.
So here's the situation:
I have an express server setup with the following files:
Express App
server.js
index.html
and right now I want the html folder to have a button, that calls a function set in the node.js file.
Tell me if more information is needed, thanks!
EDIT:
Im remaking the question to be more clear.
I am using an express server to present a website, that present a button saying " Power Off", I want this button to be able to execute an action on my server computer, that action being a terminal command to power it off.
I wanted to know how could I make said button, written in HTML, hosted on the server but presented to the client, to interact with the server.js file hosted on the server, which would have a function set to execute said command.
thanks!
You need to understand a little better how the client/server architecture of a web page works and where code actually runs and how the client and server communicate with one another.
You can't call a function directly on your node.js server from an HTML file. The HTML file is in the client's browser. The node server is your web server, far away from the client's browser on different computers. Though it may seem like your HTML is on your node.js server because it's in a directory on that server, that's only where it is stored. When the browser requests that page, your node.js server sends the HTML to the client's browser and it is rendered back in the client browser and that's where the Javascript in that page runs (in the client's browser, far away from your node.js server). This is a client-server architecture. The HTML page is running on the client. The node.js server is on your server - different computers.
If you want to communicate with the node.js server from the web page, then you use Javascript in the HTML page to make an Ajax call from the Javascript in the web page to the node.js server (An Ajax call is an http request). You then configure a route in the node.js server for that specific Ajax call and you can then write code in node.js to do whatever you want to happen when that Ajax call is received. It can carry out some operation on the server, it can retrieve data and return it to the client, etc... You can optionally send data with the Ajax call (either as query parameters for a GET request or as body data for a POST request) and then the server can optionally return data back to you (often as JSON, but it can be any format you like).
I'm not an expert with Node, but I think what's happening here is blurring the lines between server and client. Even though both use JavaScript, there's a distinction. NodeJS could easily be replaced with Ruby, PHP, Java, whatever-backend-language-you-like, and this distinction would apply in the same way it does when you use JavaScript on the server. There's no differerence.
Server-side code executes on the server. Client side code executes on the client (the browser). If you need to call a NodeJS function (assuming it has to interact with the other server side code such as databases etc) then you can send a request, either via AJAX or standard HTTP, to the a route on the server and call that function within the route.
On the other hand, if the function is generic enough and doesn't involve any specific Node code then you can simply add a script tag with your JavaScript file to the index.html page.
There is a difference between server and client. You cannot just call a function on a server from a client directly, there is more to just that. If you wanted to, you could do it by routing a path to wherever and making an HTTP request to that path, or even, using other protocols like WebSocket if you need to communicate both ways.

bringing in an API (xml)/URL into php for Joomla

what I need is a way to bring in this: 184.173.18.228/api/profile/1000/0/Adam.Adams (which is an xml file) into php ( Im assuming) to be used in Joomla ( I can use Sourcerer for the code in Joomla) - the problem I have been running into is that there is a cross domain proxy problem - over last weekend I was able to render that xml when it was locally on my machine using ajax/jquery:
// jQuery script
$(function() {
// Load data from proxy.php using GET request
$.get('test.xml', function(data)
{
// Search for the XML element you want, perform an action on each occurrence of found element
$(data).find("XMLElement").each(function()
{
$('#output').append($(this).attr('XMLAttribute')); // Display desired attribute of element -OR-
$('#output').append($(this).children("Phone").text());
I'm not even sure if Im asking the question correctly - what I would like to happen is: a php script to bring in that xml/url to echo/populate html on a page. I have tried a million things and just can't get it
That really looks like Javascript, not php. In php you wouldn't have the cross domain limitation anyway since it's running on the server, not the browser.
All browsers will block cross domain ajax calls.
Your solution is to write a script in php that does a wget or curl to the remote site, so you invoke a local script and the script on your server loads the remote url. You should be able to find some ready-made.

How do I read and write data to/from COM port in HTML pages?

Has anyone tried reading and writing data to/from COM ports in webpages. I am running Apache webserver. Could anyone one suggest me about what scripting should I use to access data in html pages from COM ports on the system ?
Thanks
There is currently no way to do this directly from HTML 5 (except perhaps for maybe on the blackberry tables OS or BB10 using webworks (would also be client side), but I have not personally tried this). If you are trying to read serial data on the server and showing the result in an HTML document it's quite easily done using javascript to sent post requests to your web-server. doing the work on the server and responding, and interpreting the server's response in the javascript.
There's lots of online guides for sending post requests from javascript asynchronously, for instance: JavaScript: Sending POST, redirecting to response.
On the server side, lots of options available. You can use PHP-serial to read serial data: http://code.google.com/p/php-serial/, or if it's not a PHP server there are libraries available for most languages. Or, if you don't like PHP-serial you can use your server language to call another executable/script on the system to do the serial reading (that external program can relay the read data via stdout or file output). Either way, the webserver can then respond to the HTTP request simply by printing out the data, and your client-side javascript can read the response body to retrieve that data.

How do I call a Lua script from an HTML5 script/file/page

I want to create web pages with dynamic content. I have an HTML page, and I want to call a lua script from it
How do I invoke the lua script?
<script type="text/application" >?
<script type="text/lua" >?
Retrieve data from it? Can I do something like:
int xx = 0;
<script type=text/lua>
xx = 123;
</script>
and have any hope that xx will be 123 when the script exits?
Replace the current web page with the content generated by the lua script.
On the client-side, you can use:
Fengari, a Lua VM written in JavaScript
WebAssembly with wasm_lua
lua.js compiles Lua directly to JavaScript. Has lower compatibility but lower footprint too.
moonshine
Fengari and Moonshine execute compiled Lua bytecode. They are more compatible than lua.js, and have a lower code-size footprint than e.g. an Emscripten-compiled Lua interpreter. They may be the slowest method of all because they aren't using WASM/asm.js like the stock Lua interpreter compiled with Emscripten would, and they aren't generating JavaScript which could be subsequently JIT'ed.
I'd try using Fengari first as it seems to be most active. It has easier JS interop than something using WASM would have.
On the WWW scripts can run in two places.
In the web browser
On the web server
If you want it to run in the browser, then you need support for the language built into the browser (or provided by an extension). For all practical purposes, if you are writing webpages for the WWW, then the only language you can use in an HTML <script> is JavaScript.
If you want to run it on the web server, then you need to get your HTTPD to run the script in response to a URL being requested from it. The simplest way to achieve this is via CGI.
With CGI, the HTTPD will run a program (as a separate process) in response to the request being made. It will pass in various information about the request via STDIN and environment variables (as described in the CGI specification). The script then prints an HTTP response (header (at least a Content-Type) and body (e.g. an HTML document)) and sends it to STDOUT where the HTTPD picks it up and sends it back to the browser.
How you configure your server to run programs using CGI depends on the server. Apache has a guide for their server.
There are probably CGI libraries for Lua, but I don't know the language so cannot make any recommendations.
CGI is a slow and inefficient protocol (as it requires a new processes to be spawned for each request). There are alternatives, such as FastCGI and various language specific options. Again, I don't know what is considered optimal in Lua land.
Most of the CGI and Lua I have played with involve generating the web page and inserting the dynamic bits, rather than calling a script from a web page. So more like option C from your original question. Any HTML 4 or 5 elements you want to have could easily be added into the generated web page.
Here are some places you can check out for more detailed information:
CGILua has some good information on how to use CGI and Lua together.
This long forum page has some good examples with code and output.
The Beginning Lua Programming book has a whole chapter walking through how to set up and use CGI and Lua. (Chapter 15 - Programming for the Web)
(While some of these places are a bit dated, they still do a good job of showing how to do this sort of thing.)
Remember: If you are using cgi or fastcgi on the server side you will need the first line of your Lua file to have a pointer to wherever the Lua interpreter is, such as:
#!/usr/local/bin/lua
If you want to run Lua in browser, consider using Lua Alchemy — Lua interpreter for Flash VM.
There are also several JavaScript implementations of Lua, but they were not mature enough last time I looked.
If you want to run Lua on server, consult this answer: What web server to use for Lua web development
Lua won't run natively in the browser; JavaScript is the only scripting library
However, you can use the JavaScript library "Starlight", which implements Lua.
You can use it like this:
<!DOCTYPE html>
<html>
<body>
<!-- WARNING! This will only work if you load the starlight library later -->
<script type="application/lua">
print('Hello world')
</script>
<!-- Here we load the library for Lua -->
<script src="//cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script>
<script src="http://paulcuth.me.uk/starlight/browser-lib/starlight.js" data-run-script-tags></script>
</body>
</html>
If you want to run a script from a browser, consider using javascript instead.
It is very similar to Lua, and unlike Lua it's interpreted by most browsers.