Call a node.js function inside an html file - html

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.

Related

Http redirect for content on relative paths

Essentially what my use case is, a 3rd party server only support POST on a specific integration url. but I want a browser to hit it from a normal html link (I have no control over either systems code, I can only configure the destination url for the link)
To solve or this I have written a web hosted app (done in Mirth Connect - but the server tech in theory shouldn't matter). The objective of my app is to cat the GET and convert it to a POST
My systems logic:
My web server receives an HTTP GET from a browser, grabs the query strings.
The server then performs an HTTP POST on a 3rd party server, and grabs the html result
The server then returns the original html and delivers it as the result to the original http request from the browser
This works great, the issue comes in with content hosted on the 3rd party server that is referenced with a relative path (css, js, images, etc).
Because I have "tricked" the browser into thinking it received the html from my system, it looks on my server for the content (which will all 404).
Without having to handle the fetching of all the content myself, is there a way to tell a browser to redirect all further queries to the 3rd party server?
I tried making my HTTP GET return a status 301 or 302 with the location being the base address of the 3rd party server, but this obviously tells the browser to redirect completely
Figured it out.
I just had to intercept the html and inject a BASE tag .

Displaying Textual Data from a remote webserver's jQuery.getJSON() script

I'm working on a personal project to display data from a remote server, the HTML code contains
the something like the following:
<script>
jQuery.getJSON("http://someurl.com/something.cgi?
AJZ=1&STOCK=S",
function(data) {
if (data.information[0]) {
var returnHTML = "<table style=\"border: 1px solid #E0E0E0;border-collapse: collapse;\"
It works fine on the remote server that calls the script, but I'm trying to just copy the data fields (they are 4 or 5 lines of textual data). I've attempted to write a simple perl script with use LWP::UserAgent and HTTP::Request::Common to fetch it to a html page, but the script data obviously needs a browser to execute.
The text displays fine when I visit the page, but when I try to incorporate it, I see a 'NOT AUTHORIZED'
Anyone have any tips on how to accomplish this?
Sounds like its a CORS (Cross-Origin Resource Sharing, http://www.w3.org/TR/cors/) problem, your remote server needs to allow cross domain access. Most servers are set up not to accept XMLHttpRequests from domains outside of their own, its a security measure to stop cross-domain scripting.
Or you could call a script on your local machine, a proxy script, that retrieves the data remotely for you and returns it to your javascript call.
Fetching a web page in Perl using LWP::Simple - http://www.onperl.net/Fetching-a-Web-Page-Module-LWP-Simple

Displaying remote URL

First I must explain I am a total newbie with regards to web design.
My question is as follows:
I would like to have a remote URL displayed through a different web server. The remote URL resides on an internal firewalled server and I would like to give public access to a single page by displaying it on a remote web server that has access to the firewalled page. I have tried iframes but they use the clients IP which results in the page failing to display. I have limited access to the server (CPanel) - please advise how this is possible? The remote URL will be requiring a login - not sure if this will have relevance on the solution.
What you can do is create a page which makes a request to the firewalled page using either CURL or HttpWebRequest or any compatible technology based on the platform you have chosen. It can then trim out the headers and other tags which are not required and render the html in a div, or it can just redirect the entire code in the response of your page.
This way, there will be no connection made from the client end, just your server will connect to your firewalled server and fetch the page from there and in turn give it back.
The only problem here is, forms - images and linked objects might not work properly, you might also have to parse them and replace the respective urls to point to your server which in turn proxyfy it.
Here is an example of it
https://proxify.net/

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.

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