简体   繁体   中英

GET request of an object from server

I'm using vanilla Javascript with Node (HTTP) for server side and XMLHttpRequest for client side. 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. using HTTP for learning purpose.

Use URL module to parse request URL to write logic for individual endpoints.

In your case the endpoint would be /data . Here you can return the data array as response.

Refer URL | Node.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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