Home > Enterprise >  express http response object does not return data
express http response object does not return data

Time:10-05

I am attempting to write a server using express js, but when I send a get request to my endpoint from another server, the response is receieved and is successful, but does not contain the data that was sent by the server after res.send(data).

here is the server

const express = require("express");
const app = express();
const cors = require("cors");
const corsOptions = {
    origin: ["http://localhost:6000"],
    optionsSuccessStatus: 200,
    credentials: true,
};
app.use(cors(corsOptions));
app.use(express.json());

app.get("/test", (req, res) => {
    res.send("hello")
})

app.listen(4000)

and here is the server that is sending the request

import fetch from "node-fetch";
const url = "http://localhost:4000/test"
import express from "express";
const app = express()

fetch(url)
.then(response => {
    console.log(response)
})
.catch(err => {
    console.log(err)
})


app.listen(6000)

the response which is returned is not the string sent from the server, but rather the object containing the http response (which is too large to put here), but without the original data, though it specifies a response status of 200 which suggests that the data was sent.

strangely, the server does return the original data when the request is sent from the command prompt using curl, but the CORS headers explicitly contain the localhost port of the request. Am I missing an important piece of information about CORS, or is this error the result of something else?

CodePudding user response:

Looks like you are logging out the fetch response which will look like a huge object with tons of incomprehensible data.

You have to call a method on that response to see the actual server response data. There is response.json() and response.text() to name 2 of them. See docs

Try:

fetch(url)
.then(response => response.text())
.then(response => {
    console.log(response)
})
.catch(err => {
    console.log(err)
})

CodePudding user response:

fetch() by itself returns a promise that resolves only to the response object which includes the headers and the open stream, but NOT the body of the response. It has not yet read the actual response body. To do that, you need to call one of the methods that reads the body such as .text() or .json().

import fetch from "node-fetch";
const url = "http://localhost:4000/test"

fetch(url).then(response => {
    return response.text();       // <==== tell it to read the body here
}).then(result => {
    console.log(result);
}).catch(err => {
    console.log(err)
});

Or, it sometimes looks a little clearer if you use async/await:

import fetch from "node-fetch";
const url = "http://localhost:4000/test"

async function run() {
    let response = await fetch(url);     // gets headers
    let text = await response.text();    // reads the actual body
    return text;
}

run().then(text => {
    console.log(text);
}).catch(err => {
    console.log(err);
});
  • Related