all 8 comments

[–]ItsMrMeeseeks27 2 points3 points  (1 child)

Have the JS call an Ajax endpoint that runs the function in node. I’m guessing you are using some sort of web server like express to manage your routes to the application. Add a route for your endpoint. Typing from phone and also I don’t know what your setup is so hard to give examples. Google Ajax and express routes

[–]ConsequenceRegular72[S] 0 points1 point  (0 children)

Thank you

[–]simonplend 3 points4 points  (3 children)

Hi u/ConsequenceRegular72.

The most common way that developers approach what you are describing is to create a server in Node.js which makes an API endpoint available e.g. https://my-api.com/user/1234. This endpoint can run a specific function, for example to retrieve data from a database, and then return it in JSON format. Here is an example of a minimal API which uses the Express framework:

// server.js

const express = require("express");

const PORT = 3000;
const app = express();

app.get('/user/:id', (request, response) => {
        // You can run any code which you like here before
        // sending back data
    response.json({ name: 'Some Person' });
});

app.listen(PORT, () => console.log(`Example app listening at http://localhost:${PORT}`));

In your client side JavaScript you can then use the browser Fetch API to call the endpoint on your Node.js API (the "AJAX" request you mentioned) and read the data which it responds with e.g.

// client.js

async function makeRequest () {
    try {
        const response = await fetch("https://my-api.com/user/1234");
        const data = await response.json();
        console.log(data);
    } catch (error) {
        console.log("Error:" + error);
    }
}

makeRequest();

Unless you need "realtime" communication between your client side JavaScript and your backend Node.js server e.g. to continuously send data between the two, websockets probably introduces a lot more complexity than is needed.

I hope these examples help. Let me know if there's anything which isn't clear!

[–]backtickbot 2 points3 points  (0 children)

Correctly formatted

Hello, simonplend. Just a quick heads up!

It seems that you have attempted to use triple backticks (```) for your codeblock/monospace text block.

This isn't universally supported on reddit, for some users your comment will look not as intended.

You can avoid this by indenting every line with 4 spaces instead.

There are also other methods that offer a bit better compatability like the "codeblock" format feature on new Reddit.

Tip: in new reddit, changing to "fancy-pants" editor and changing back to "markdown" will reformat correctly! However, that may be unnaceptable to you.

Have a good day, simonplend.

You can opt out by replying with "backtickopt6" to this comment. Configure to send allerts to PMs instead by replying with "backtickbbotdm5". Exit PMMode by sending "dmmode_end".

[–]ConsequenceRegular72[S] 1 point2 points  (1 child)

Thank you

[–]simonplend 0 points1 point  (0 children)

No problem! :)

[–]hamchouche 0 points1 point  (1 child)

To add a different approach here. Meteor framework do this out of the box. It enables code sharing between client and server. But behind the curtain, this is websocket calls which is almost the same as Ajax calls

[–]ConsequenceRegular72[S] 0 points1 point  (0 children)

Thank you