6

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.

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.

4
  • You can add the js file into html body part using following: <script src="myscripts.js"></script> Commented Sep 1, 2016 at 21:04
  • You can execute JavaScript (the language implemented by Node.js) in a browser rendering an html page using the <script> tag Commented Sep 1, 2016 at 21:05
  • 1
    Maybe you should start with understanding the difference between client and server. Commented Sep 1, 2016 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 <h1><%=foo()%></h1> 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.... Commented Sep 1, 2016 at 21:26

3 Answers 3

16

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

Sign up to request clarification or add additional context in comments.

11 Comments

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.
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!
would it help me to understand?
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.
|
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.