Triggering a function from client side Node.js

Hello Everybody,

I am a new bee in the Node JS and I want to ask a question about “triggering a function on the server side”.

I am using “axios” library for post/get requests. And node js require(‘axios’) statement only runs on the main.js(entry file).

So in my scenario, from the client side I want to create a button and trigger the function that includes post/get requests which is inside main.js file.

How can I manage it? Could anybody help me about it.

Thanks,

I have to say that your questions have me a bit perplexed. Node.js doesn’t have your traditional user interface so the concept of a button and such is done with whatever front end client side framework you are using (like React). A button in react would call a handler function that would make your axios calls directly or call over to your node.js app and have node then trigger the axios calls.

Now let’s say you wrote a function in node that you then want to call from the client. This is where you would have to essentially do post/get requests to your node application through something like Express or some other web service functionality. These requests would just be like calling an API for some other service.

That is how you traditionally get some front end like react, which is running on the client, to call over to your server-side code. Always remember, when we talk client and server-side, WHERE the code is actually executing.

For an example of calling Axios functions in something like React you can check out this tutorial…

Hopefully I am making that clear enough.

Thanks for the response,
I have get into deep more by the time and I have chosen the option like;
client side script is -------> sending the button trigger
and app.js function <-----receiving the button click evet from the client script.

however I couldn’t solve a problem about the node js architecture. In order to accomplish my scenario I need to share the libraries from one .js script file to another.

Importing libraries and files is done in node js with require statement, like:
const axios = require (‘axios’). I have downloaded two simple project sample for node js. In both projects, you can only use require in app.js (main file of node js). It seems weird to me. Do I have to do everything in just one file.

I know that, require statement, can be used in other files. Could you explain how can I share modules like axios from one script file to another. What is the key point of using require statement in multiple files. If you help about it, I would be very happy,

Thanks,

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.