简体   繁体   English

使用Node.js在服务器端保存客户端数据

[英]Saving client data on the server side using Node.js

I have been experimenting with Node.js on my machine for a little while and I have found my knowledge of HTTP requests, XHR objects, and the like quite lacking. 我已经在机器上尝试了Node.js一段时间,发现我对HTTP请求,XHR对象等的知识非常缺乏。 So the main thing that's been nagging at me while using Node is that I can't seem to understand how to communicate from the client to the server (other than simple GET requests) and vice versa. 因此,在使用Node时一直困扰我的主要事情是,我似乎无法理解如何从客户端到服务器进行通信(除了简单的GET请求),反之亦然。 What brings me to this question is my recent project, which is a simple 2 player chess game (no AI opponents). 让我想到这个问题的是我最近的项目,这是一个简单的2人棋牌游戏(没有AI对手)。

What I want to do is to be able to send the game board data (a JSON string) to the server and have it save the data to a file. 我要做的是能够将游戏板数据(JSON字符串)发送到服务器,并将其保存到文件中。 I understand how to get the file contents using an XHR object on the client-side. 我了解如何在客户端使用XHR对象获取文件内容。 I also understand how to use Node's fs module to create and read files on the server-side. 我也了解如何使用Node的fs模块在服务器端创建和读取文件。 What I don't understand is how to use the XHR object to send the string to the server and have Node process and save it into a file. 我不明白的是如何使用XHR对象将字符串发送到服务器并进行Node处理并将其保存到文件中。 Is it even possible to call server-side code using client-side code in this way? 甚至可以通过这种方式使用客户端代码调用服务器端代码吗? Is trying to send an argument through a XHR object to the server an incorrect way of doing this? 试图通过XHR对象向服务器发送参数是一种错误的方法吗?

If what I have asked is too broad of a topic to answer, I would also be open to links and books on the topic of server and client communication. 如果我所要回答的主题范围太广,我也可以公开有关服务器和客户端通信主题的链接和书籍。

You can can use XHR to send an HTTP request with (eg) JSON data in the POST payload. 您可以使用XHR在POST有效负载中发送带有(例如)JSON数据的HTTP请求。

You can then write server-side Node.js code that handles this request, reads the payload, and does whatever you want with the data. 然后,您可以编写服务器端Node.js代码来处理此请求,读取有效负载并根据数据执行所需的任何操作。

Expanding on SLaks answer a little: 在SLaks上扩展会回答一些问题:

Assuming you're using jQuery on the client and express on the server (both are useful frameworks to avoid reinventing low-level stuff), you could do something like this. 假设您正在客户端上使用jQuery并在服务器上进行表达 (这都是避免重新创建低级内容的有用框架),则可以执行以下操作。

Client 客户

$.ajax({
  type: "POST",
  url: "http://www.yourserver.com:3000/some/path",
  data: { ...chessdatahere... }
}).done(function(msg) {
  alert("Data Saved: " + msg);
});

Server 服务器

var fs = require('fs');
var express = require('express');
var bodyParser  = require('body-parser');
var app = express();

app.use(bodyParser.json());
app.post('/some/path', function(req, res) {
  fs.writeFile('filename', res.body, function(err) {
    if (err) {
      res.send('Something when wrong');
    } else {
      res.send('Saved!');
    }
  })
});

app.listen(3000);

(Note that the code has not been tested, but it should show the general idea at least). (请注意,该代码尚未经过测试,但至少应显示总体思路)。

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

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