Call a node.js function inside an html file -


this repeated question.

the closest got answer here: execute nodejs script html page?

yet, still can't understand.

so here's situation:

i have express server setup following files:

  • express app

    server.js index.html

and right want html folder have button, calls function set in node.js file.

tell me if more information needed, thanks!

edit: im remaking question more clear.

i using express server present website, present button saying " power off", want button able execute action on server computer, action being terminal command power off.

i wanted know how make said button, written in html, hosted on server presented client, interact server.js file hosted on server, have function set execute said command.

thanks!

you need understand little better how client/server architecture of web page works , code runs , how client , server communicate 1 another.

you can't call function directly on node.js server html file. html file in client's browser. node server web server, far away client's browser on different computers. though may seem html on node.js server because it's in directory on server, that's stored. when browser requests page, node.js server sends html client's browser , rendered in client browser , that's javascript in page runs (in client's browser, far away node.js server). client-server architecture. html page running on client. node.js server on server - different computers.

if want communicate node.js server web page, use javascript in html page make ajax call javascript in web page node.js server (an ajax call http request). configure route in node.js server specific ajax call , can write code in node.js whatever want happen when ajax call received. can carry out operation on server, can retrieve data , return client, etc... can optionally send data ajax call (either query parameters request or body data post request) , server can optionally return data (often json, can format like).


Comments