在节点。如何与客户端JavaScript进行通信?

时间:2022-06-20 15:59:51

For example suppose client side I have a JavaScript function

例如,假设客户端我有一个JavaScript函数。

function getLocation() {
    var latitude = ...;
    var longitude = ...;
}

The coordinates are retrieved using a function call to Google or similar. How can I transmit this information to my Node.js server?

使用对谷歌或类似的函数调用来检索坐标。如何将这些信息传输到我的节点。js服务器?

5 个解决方案

#1


12  

The easiest way? Set up Express and have your client side code communicate via Ajax (for example, using jQuery).

最简单的方法吗?设置Express并让您的客户端代码通过Ajax进行通信(例如,使用jQuery)。

(function() {
  var app, express;

  express = require("express");

  app = express.createServer();

  app.configure(function() {
    app.use(express.bodyParser());
    return app.use(app.router);
  });

  app.configure("development", function() {
    return app.use(express.errorHandler({
      dumpExceptions: true,
      showStack: true
    }));
  });

  app.post("/locations", function(request, response) {
    var latitude, longitude;
    latitude = request.body.latitude;
    longitude = request.body.longitude;
    return response.json({}, 200);
  });

  app.listen(80);

}).call(this);

On the client side, you might call it like this:

在客户端,你可以这样称呼它:

var latitude = 0
  , longitude = 0; // Set from form

$.post({
  url: "http://localhost/locations",
  data: {latitude: latitude, longitude: longitude},
  success: function (data) {
    console.log("Success");
  },
  dataType: "json"
});

Note this code is simply an example; you'll have to work out the error handling, etc.

注意,这段代码只是一个例子;你必须解决错误处理等问题。

Hope that helps.

希望有帮助。

#2


4  

By making an HTTP request, just like you would with any other server side program in a web application.

通过发出HTTP请求,就像在web应用程序中使用任何其他服务器端程序一样。

You could do this with the XMLHttpRequest object, or by generating a <form> and then submitting it, or a variety of other methods.

您可以使用XMLHttpRequest对象,或者通过生成一个

,然后提交它,或者其他各种方法。

#3


2  

If you need (soft) real-time capabilities, I recommend using the Socket.io library. With socket, node can also push data to your client side scripts.

如果您需要(软)实时功能,我建议您使用套接字。io库。使用套接字,node还可以将数据推送到客户端脚本。

#4


1  

I think that NowJS will be a perfect fit for you. Example:

我觉得现在秀的很适合你。例子:

// On the Node.JS server

var nowjs = require("now");
var everyone = nowjs.initialize(httpServer);

everyone.now.getServerInfo = function(callback){
  db.doQuery(callback);
}

// In the browser

<script>

now.getServerInfo(function(data){
  // data contains the query results
});

</script>

You can put variables and functions in a common namespace (i.e. the now object), from the client to the server and the other way around.

您可以将变量和函数放在一个公共名称空间(即现在的对象)中,从客户机到服务器,以及其他方式。

#5


0  

I think you need RPC. The node modules section also has a section covering RPC. I think you should have a look at DNode. Have a look at the DNode on the browser section.

我认为你需要RPC。节点模块部分还有一个涉及RPC的部分。我想你应该看看DNode。查看浏览器部分的DNode。

#1


12  

The easiest way? Set up Express and have your client side code communicate via Ajax (for example, using jQuery).

最简单的方法吗?设置Express并让您的客户端代码通过Ajax进行通信(例如,使用jQuery)。

(function() {
  var app, express;

  express = require("express");

  app = express.createServer();

  app.configure(function() {
    app.use(express.bodyParser());
    return app.use(app.router);
  });

  app.configure("development", function() {
    return app.use(express.errorHandler({
      dumpExceptions: true,
      showStack: true
    }));
  });

  app.post("/locations", function(request, response) {
    var latitude, longitude;
    latitude = request.body.latitude;
    longitude = request.body.longitude;
    return response.json({}, 200);
  });

  app.listen(80);

}).call(this);

On the client side, you might call it like this:

在客户端,你可以这样称呼它:

var latitude = 0
  , longitude = 0; // Set from form

$.post({
  url: "http://localhost/locations",
  data: {latitude: latitude, longitude: longitude},
  success: function (data) {
    console.log("Success");
  },
  dataType: "json"
});

Note this code is simply an example; you'll have to work out the error handling, etc.

注意,这段代码只是一个例子;你必须解决错误处理等问题。

Hope that helps.

希望有帮助。

#2


4  

By making an HTTP request, just like you would with any other server side program in a web application.

通过发出HTTP请求,就像在web应用程序中使用任何其他服务器端程序一样。

You could do this with the XMLHttpRequest object, or by generating a <form> and then submitting it, or a variety of other methods.

您可以使用XMLHttpRequest对象,或者通过生成一个

,然后提交它,或者其他各种方法。

#3


2  

If you need (soft) real-time capabilities, I recommend using the Socket.io library. With socket, node can also push data to your client side scripts.

如果您需要(软)实时功能,我建议您使用套接字。io库。使用套接字,node还可以将数据推送到客户端脚本。

#4


1  

I think that NowJS will be a perfect fit for you. Example:

我觉得现在秀的很适合你。例子:

// On the Node.JS server

var nowjs = require("now");
var everyone = nowjs.initialize(httpServer);

everyone.now.getServerInfo = function(callback){
  db.doQuery(callback);
}

// In the browser

<script>

now.getServerInfo(function(data){
  // data contains the query results
});

</script>

You can put variables and functions in a common namespace (i.e. the now object), from the client to the server and the other way around.

您可以将变量和函数放在一个公共名称空间(即现在的对象)中,从客户机到服务器,以及其他方式。

#5


0  

I think you need RPC. The node modules section also has a section covering RPC. I think you should have a look at DNode. Have a look at the DNode on the browser section.

我认为你需要RPC。节点模块部分还有一个涉及RPC的部分。我想你应该看看DNode。查看浏览器部分的DNode。