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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11115508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:07:13  来源:igfitidea点击:

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

javascriptnode.js

提问by Hoa

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?

使用对 Google 或类似方法的函数调用来检索坐标。如何将此信息传输到我的 Node.js 服务器?

回答by Matthew Ratzloff

The easiest way? Set up Expressand 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 Quentin

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>然后提交它,或者各种其他方法来做到这一点。

回答by Sammy S.

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.

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

回答by Gabi Purcaru

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

我认为NowJS将非常适合您。例子:

// 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 nowobject), from the client to the server and the other way around.

您可以将变量和函数放在一个公共命名空间(即now对象)中,从客户端到服务器,反之亦然。

回答by Alfred

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