5

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!

Community
  • 1
  • 1
hiperbolt
  • 184
  • 2
  • 3
  • 11
  • You can add the js file into html body part using following: `````` – Josan Sep 01 '16 at 21:04
  • You can execute JavaScript (the language implemented by Node.js) in a browser rendering an html page using the ` – fvgs Sep 01 '16 at 21:05
  • 1
    Maybe you should start with understanding the difference between client and server. – Amberlamps Sep 01 '16 at 21:05
  • 1
    Not a `node` person but the OP could also mean "is there a way to emit server side methods, _inline_ html" just as one would in other "frameworks" like Pph, ASP..Net etc. So something like `

    <%=foo()%>

    ` would be how I'd do it in ASP/.Net for example ("calling a `C#` or `VB` `foo()` function") in HTML (view)...skipping "extra" Ajax calls (it's rendered without extra client-side call). I'd be interested in learning too....
    – EdSF Sep 01 '16 at 21:26

3 Answers3

15

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

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Well I have a pretty good experience with python, It's just my first time using node. I understand the difference between server and client well lo, i'd like to think so. – hiperbolt Sep 01 '16 at 22:44
  • Theoretically, how would I go about having my html page with a button, that button calling a function in my server-side javascript file, said function would execute a terminal command. Thanks mate! – hiperbolt Sep 01 '16 at 22:45
  • And I found this : http://mherman.org/blog/2013/10/20/handling-ajax-calls-with-node-dot-js-and-express-scraping-craigslist/#.V8ivoe-aFhE – hiperbolt Sep 01 '16 at 22:46
  • would it help me to understand? – hiperbolt Sep 01 '16 at 22:46
  • @HiperBolt - I already explained in my answer - please read my answer again. You use an Ajax call from browser web page to your server. If you don't understand something I explained in my answer, please ask more specifically about what exactly you don't understand. Do you know what an Ajax call is? Do you know how to create a route in node.js to handle an Ajax request? Do you know how to make an Ajax call from web page Javascript? FYI, an Ajax request is just an http request from a browser to a server somewhere. See: https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started – jfriend00 Sep 01 '16 at 22:58
  • @HiperBolt - And an example: http://code.runnable.com/VeRzEU2VvyQwPb-4/ajax-with-jquery-demo-for-express-and-node-js, – jfriend00 Sep 01 '16 at 23:05
  • Could we take this to pm so you could help me out understand better :) ? – hiperbolt Sep 01 '16 at 23:25
  • 1
    No, that's not how this works here. I write an answer. If you don't understand something in it, you write a comment that explains what exactly in the answer you don't understand and then I try to clarify my answer around the things you don't understand. The end result of this site is supposed to be a clear question and one or more clear answers all written in text. This is not a private discussion Q&A site. If you need help understanding something, tell us what exactly you don't yet understand so we can clarify our answers. – jfriend00 Sep 01 '16 at 23:38
  • Makes all sense, i appologize, I'll better try to explain my cenario. – hiperbolt Sep 01 '16 at 23:42
  • I have updated my question, please clarify your answer accordingly – hiperbolt Sep 02 '16 at 12:25
  • @HiperBolt - The last paragraph of my answer explains what you need to do. You make an Ajax call from Javascript in the browser to your server and instruct the server to do something on your behalf. – jfriend00 Sep 02 '16 at 15:24
3

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.

Luke K
  • 865
  • 6
  • 13
0

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.