V
V
Vladislav Molchanov2017-02-22 01:40:29
Node.js
Vladislav Molchanov, 2017-02-22 01:40:29

Why is the server not returning anything to the client?

I am learning ajax and want to test it. To do this, I wrote a simple node.js server that reads the file and gives the client information from it as a string.

Source code from the client side:

"use strict";

(function() {

    var xhr = new XMLHttpRequest();

    xhr.open("GET", "http://127.0.0.1:1534/test.txt");
    xhr.setRequestHeader("Access-Control-Allow-Origin", "*");

    xhr.addEventListener("readystatechange", function(event) {
        if (xhr.readyState == 4) {
            console.log("connection is successful");
        }
        console.log(xhr.response);
    });

    xhr.send(null);

})();


Source code from server side:
"use strict";

var http = require("http");
var url = require("url");
var fs = require("fs");

var server = new http.Server(function(req, res) {

  switch (req.url) {

  case "/test.txt":
    res.setHeader("Cache-control", "no-cache");
  		res.end("OK");

    fs.readFile("test.txt", function(err, info) {
      if (err) {
        console.log(err);
        res.statusCode = 500;
        res.end("Server error");
      }
      console.log(info.toString());
      res.end(info.toString());
    });
    break;

  default: 
    res.statusCode = 404;
    res.end("File not found :(");
  }

});

server.listen(1534, '127.0.0.1');


When I load the page, the contents of the file are displayed in the terminal from which the server was launched. Nothing happens in the browser console. Tell me, please, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Burov, 2017-02-22
@vmolchanov

you have twice called res.end
and
"Access-Control-Allow-Origin:*"Should give the server, not the client to send.
client.js:

"use strict";

(function() {

    var xhr = new XMLHttpRequest();

    xhr.open("GET", "http://127.0.0.1:1534/test.txt");
    xhr.addEventListener("readystatechange", function(event) {
        if (xhr.readyState == 4) {
            console.log(xhr.response);
        }
    });
    xhr.send(null);
})();

server.js:
"use strict";

var http = require("http");
var url = require("url");
var fs = require("fs");

var server = new http.Server(function(req, res) {

      switch (req.url) {

                case "/test.txt":
                  res.setHeader("Cache-control", "no-cache");
                  res.setHeader("Access-Control-Allow-Origin", "*");

                  fs.readFile("test.txt", function(err, info) {
                            if (err) {
                                        console.log(err);
                                        res.statusCode = 500;
                                        res.end("Server error");
                                      }
                            console.log(info.toString());
                            res.end(info.toString());
                          });
                  break;

                default: 
                  res.statusCode = 404;
                  res.end("File not found :(");
                }

});

server.listen(1534, '127.0.0.1');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question