javascript - Saving client data on the server side using Node.js - Stack Overflow

admin2025-04-19  1

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. So the main thing that's been nagging at me while using Node is that I can't seem to understand how to municate from the client to the server (other than simple GET requests) and vice versa. What brings me to this question is my recent project, which is a simple 2 player chess game (no AI opponents).

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. I understand how to get the file contents using an XHR object on the client-side. I also understand how to use Node's fs module to create and read files on the server-side. 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. 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?

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 munication.

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. So the main thing that's been nagging at me while using Node is that I can't seem to understand how to municate from the client to the server (other than simple GET requests) and vice versa. What brings me to this question is my recent project, which is a simple 2 player chess game (no AI opponents).

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. I understand how to get the file contents using an XHR object on the client-side. I also understand how to use Node's fs module to create and read files on the server-side. 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. 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?

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 munication.

Share Improve this question asked Aug 4, 2014 at 20:03 the_madnessthe_madness 211 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Expanding on SLaks answer a little:

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.

Client

$.ajax({
  type: "POST",
  url: "http://www.yourserver.: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).

You can can use XHR to send an HTTP request with (eg) JSON data in the POST payload.

You can then write server-side Node.js code that handles this request, reads the payload, and does whatever you want with the data.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745021550a280412.html

最新回复(0)