最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What's the most efficient way to call a Node.js backend function with JavaScript - Stack Overflow

programmeradmin10浏览0评论

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.

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.

Share Improve this question edited Feb 7, 2016 at 1:51 gariepy 3,6746 gold badges23 silver badges34 bronze badges asked Dec 26, 2015 at 22:14 Calder WhiteCalder White 1,3261 gold badge9 silver badges21 bronze badges 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? – Anthony 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. – Quentin Commented Dec 26, 2015 at 22:16
  • 1 This is extremely broad, but the keywords you're probably looking for are REST API. – Pier-Luc Gendreau 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. – Bergi Commented Dec 26, 2015 at 22:59
  • @AnthonyMayfield yes, I want to call a backend function from a client browser. Any insight? – Calder White Commented Dec 26, 2015 at 23:00
Add a ment  | 

3 Answers 3

Reset to default 4

Communicating with node.js is like municating 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 );
});

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 acplish. 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 remendation 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./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.

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.:3000'})

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

const result = await api.yourApiMethod()

Try it out. Hope it helps.

发布评论

评论列表(0)

  1. 暂无评论