简体   繁体   English

从服务器获取对象的请求

[英]GET request of an object from server

I'm using vanilla Javascript with Node (HTTP) for server side and XMLHttpRequest for client side.我在服务器端使用带有 Node (HTTP) 的 vanilla Javascript,在客户端使用 XMLHttpRequest。 Not sure how i could send an object that i've read on the server to the client.不知道如何将我在服务器上读取的对象发送到客户端。

For example i've this server code, to read the data and store it on data array to send later to the client:例如,我有这个服务器代码,用于读取数据并将其存储在数据数组中以便稍后发送给客户端:

            data = []
            fs.readFile("./client.json", function(err, jsonData){
                if(err){
                    throw err;
                }else{
                    data.push(JSON.parse(jsonData));
                }
            });

Now i'm kinda lost of how client side would be to GET the data array.现在我有点不知道客户端如何获取数据数组。 I've tried this but couldn't get any我试过这个,但不能得到任何

let xhttp = new XMLHttpRequest();

    xhttp.open("GET", "http://localhost:3000/data", true);
    xhttp.send(null);

I don't want to use Fetch nor Express.我不想使用 Fetch 或 Express。 using HTTP for learning purpose.使用 HTTP 进行学习。

Use URL module to parse request URL to write logic for individual endpoints.使用URL模块解析请求 URL为各个端点编写逻辑。

In your case the endpoint would be /data .在您的情况下,端点将是/data Here you can return the data array as response.在这里您可以返回数据数组作为响应。

Refer URL |参考网址 | Node.js节点.js

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

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