简体   繁体   English

在Node.js中如何与客户端JavaScript通信?

[英]In Node.js how do I communicate with client side JavaScript?

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. 使用对Google或类似的函数调用来检索坐标。 How can I transmit this information to my Node.js server? 如何将此信息传输到Node.js服务器?

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. 希望有所帮助。

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对象 ,或通过生成<form>然后提交它或其他各种方法来完成此操作。

If you need (soft) real-time capabilities, I recommend using the Socket.io library . 如果您需要(软)实时功能,我建议使用Socket.io库 With socket, node can also push data to your client side scripts. 使用套接字,节点还可以将数据推送到客户端脚本。

I think that NowJS will be a perfect fit for you. 我认为NowJS将是您的最佳选择。 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 (ie the now object), from the client to the server and the other way around. 您可以将变量和函数放在公共命名空间(即now对象)中,从客户端到服务器,反之亦然。

I think you need RPC . 我认为你需要RPC The node modules section also has a section covering RPC . 节点模块部分还有一个覆盖RPC的部分 I think you should have a look at DNode . 我想你应该看看DNode Have a look at the DNode on the browser section . 看一下浏览器部分DNode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM