1

I'm an html5 developer with mainly JavaScript experience. I'm starting to learn the backend using Node.js. I don't have a particular example of this question/requirements. I'd like to call a back end function with JavaScript, but I'm not sure how. I already researched events and such for Node.js, but I'm still not sure how to use them.

5
  • You mean you want to call a backend function in NodeJS from a client, like a browser? Or you just want to call a Node.JS function in general? Commented Dec 26, 2015 at 22:16
  • 3
    "most efficient" depends a lot on context, maybe a standard HTTP request, maybe web sockets, maybe something else. Commented Dec 26, 2015 at 22:16
  • 1
    This is extremely broad, but the keywords you're probably looking for are REST API. Commented Dec 26, 2015 at 22:23
  • Basically your node code will need to create a server that listens for connections and calls the system function when a request is received. Commented Dec 26, 2015 at 22:59
  • @AnthonyMayfield yes, I want to call a backend function from a client browser. Any insight? Commented Dec 26, 2015 at 23:00

3 Answers 3

4

Communicating with node.js is like communicating with any other server side technology.. you would need to set up some form of api. What kind you need would depend on your use case. This would be a different topic but a hint would be if you need persistent connections go with web sockets and if you just need occasional connections go with rest. Here is an example of calling a node function using a rest api and express.

var express = require('express'); 
var app = express();

app.post('/api/foo', foo);

function foo(req, res){
 res.send('hello world');
};

app.listen(3000);

From the frontend you can post to this REST endpoint like so.

$.post("/api/foo", function(data) {
  console.log( "Foo function result:", data );
});
Sign up to request clarification or add additional context in comments.

3 Comments

I did my research, and recreating this was increadibly easy, thanks.
my next question would be how do I run that listener on the same port as my server?
The easiest way would be to serve your html using the node server itself. Is that an option?
2

If you're just starting with node-js, don't worry about Websockets just yet.

You're going to want to create a REST API (most likely) depending on what you're trying to accomplish. You can put that REST API behind some kind of authentication if desired.

A REST API is going to have endpoints for creating/deleting/updating and getting (finding) a document, like a given user.

My recommendation is to work backwards from something that's already working. Clone this app locally and check out the controllers to see examples of how this application interacts with creating users.

https://github.com/sahat/hackathon-starter

Once you create a controller that returns data when a client hits an endpoint (like http://localhost:3000/user/create ) , you'll want to create some HTML that will interact with endpoint through a form HTML element. Or you can interact with that endpoint with Javascript using a library like jQuery.

Let me know if that makes sense to you. Definitely a good starting point is to clone that app and work backwards from there.

2 Comments

Thanks for the anonymous / unexplained downvote whoever that was!
really great guidance, thanks a lot! I'll check out the github link and see how it work. Really great help for a back end starter.
0

Can I suggest trying api-mount. It basically allows calling API as simple functions without having to think about AJAX requests, fetch, express, etc. Basically in server you do:

const ApiMount = apiMountFactory()
ApiMount.exposeApi(api)

"api" is basically an object of methods/functions that you are willing to call from your web application.

On the web application you then do this:

const api = mountApi({baseUrl: 'http://your-server.com:3000'})

Having done that you can call your API simply like this:

const result = await api.yourApiMethod()

Try it out. Hope it helps.

2 Comments

For the const result = await api.yourApiMethod(), that is written in the back end right? Oh and also, how would I use this if I had a button that when clicked, runs a function from the back end?
Function is implemented in back-end. It is typically called in front-end. Depending on where your back-end is - you might need to use cors setup (see npmjs.com/package/api-mount-client and search for "cors"). There is also an example how to use it in browser (see "Try it out"). You would just need to call it from click event of a button.

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.