Javascript Express server未向客户端发送响应(使用fetch)

Javascript Express server未向客户端发送响应(使用fetch),javascript,node.js,express,post,fetch,Javascript,Node.js,Express,Post,Fetch,我正在使用服务器端的express,并使用fetch从客户端向服务器发出post请求 正在发送并显示我发布到服务器的数据。服务器响应中的数据在客户端的任何位置都看不到 以下是我在服务器端的代码: app.post('/info', ensureAuthenticated, function(req,res){ console.log(req.body) var tryFetch = {myString: 'I am working fetch'}; res.setHeader

我正在使用服务器端的
express
,并使用
fetch
从客户端向服务器发出post请求

正在发送并显示我发布到服务器的数据。服务器响应中的数据在客户端的任何位置都看不到

以下是我在服务器端的代码:

app.post('/info',
ensureAuthenticated, function(req,res){
   console.log(req.body)
   var tryFetch = {myString: 'I am working fetch'};

   res.setHeader('Content-Type', 'application/json');
   res.end(JSON.stringify(tryFetch));
})
客户端如下所示:

fetch("/info", {
    method: "post",
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },

    body: JSON.stringify({
        name : e.target.parentElement.username,
        socketID : e.target.parentElement.socketID,
        id : e.target.parentElement.id,
    })
})
.then( (response) => { 
    console.log(response)
}); 
当我
console.log()
响应时,控制台显示:

Response {type: "basic", url: "http://localhost:4000/info", redirected: false, status: 200, ok: true, …}

type: "basic"
url: "http://localhost:4000/info"
redirected: false
status: 200
ok: truestatusText: "OK"
headers: Headers
__proto__: Headersbody: (...)
bodyUsed: false
__proto__: Response

我不知道这里缺少什么,无法将数据从服务器发送到客户端。有人能帮我吗?非常感谢。感谢您抽出时间

您应该
res.send
res.json
而不是
res.end

res.end()
用于在没有任何数据的情况下快速结束响应

app.post('/info',
ensureAuthenticated, function(req,res){
   console.log(req.body)
   var tryFetch = {myString: 'I am working fetch'};

   res.setHeader('Content-Type', 'application/json');
   res.send(JSON.stringify(tryFetch));
})

更新

返回json

app.post('/info',
ensureAuthenticated, function(req,res){
   console.log(req.body)
   var tryFetch = {myString: 'I am working fetch'};
   res.json(tryFetch);
})

所以,我终于找到了这个案子的症结所在

问题不在服务器上,而是在客户端和获取承诺上。事实上,当我
console.log(res.body)

ReadableStream {locked: false}
locked: false
__proto__: ReadableStream
这是对我有用的东西。我换了

.then( (response) => { 
    console.log(response)
}); 

因此,总的来说,提取必须如下所示:

fetch("/info", {
    method: "post",
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    },

    //make sure to serialize your JSON body
    body: JSON.stringify({
        name : e.target.parentElement.username,
        socketID : e.target.parentElement.socketID,
        id : e.target.parentElement.id,
    })
})
.then(response => response.json())
.then((body) => {
        console.log(body);
}); 

现在,当i
console.log()。以下更新对我有效

axios.post(url, userInfo)
            .then((req, res) => {
                console.log('Response:', res)
            })
            .catch(error => console.log("client Error:", error))
而不是.then((req,res)=>{})

更新到.then((res)=>{})


当你尝试从邮递员那里打这个电话时会发生什么?@learning每天我都从邮递员那里打这个电话,它会在体内返回json。它还返回一个cookie和6个headers不幸的是,我也尝试了这个方法,结果没有改变。响应仍然与我上面提到的相同。@我们知道为什么有这一行
res.setHeader('Content-Type','application/json')?因为我想返回json。你认为这会引起什么问题吗?@we_mor是的,你不需要那行,如果你想返回json,请看我的编辑。这也不起作用,同样的事情正在发生@1baga
axios.post(url, userInfo)
            .then((req, res) => {
                console.log('Response:', res)
            })
            .catch(error => console.log("client Error:", error))
axios.post(url, userInfo)
            .then((res) => {
                console.log('Response:', res)
            })
            .catch(error => console.log("client Error:", error))