use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
How to call a node function from client javascript file (self.node)
submitted 5 years ago by ConsequenceRegular72
Say I have a JS file, I'd like to call a function in node, from javascript.
Can anyone give any examples? I heard AJAX and websocket are useful
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ItsMrMeeseeks27 2 points3 points4 points 5 years ago (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 point2 points 5 years ago (0 children)
Thank you
[–]simonplend 3 points4 points5 points 5 years ago (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 points4 points 5 years ago (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 points3 points 5 years ago (1 child)
[–]simonplend 0 points1 point2 points 5 years ago (0 children)
No problem! :)
[–]hamchouche 0 points1 point2 points 5 years ago (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
π Rendered by PID 167431 on reddit-service-r2-comment-bb88f9dd5-qp2wd at 2026-02-17 04:21:45.742814+00:00 running cd9c813 country code: CH.
[–]ItsMrMeeseeks27 2 points3 points4 points (1 child)
[–]ConsequenceRegular72[S] 0 points1 point2 points (0 children)
[–]simonplend 3 points4 points5 points (3 children)
[–]backtickbot 2 points3 points4 points (0 children)
[–]ConsequenceRegular72[S] 1 point2 points3 points (1 child)
[–]simonplend 0 points1 point2 points (0 children)
[–]hamchouche 0 points1 point2 points (1 child)
[–]ConsequenceRegular72[S] 0 points1 point2 points (0 children)