I am fetching data from my backend using fetch function and it returns a readableStream as response's body. How can I extract the data in that readable stream?
I've tries using res.json
method, but it returns undefined.
Here's my fetch call -
fetch('/playground',{
method: 'POST',
}).then(function(res){
res.json()
}).then(data => console.log(data))
it returns undefined
in the console.
Here's the code from node.js server
router.route('/playground')
.post((req,res)=>{
res.send({message: 'Hello from backend'})
})
I was expecting it to give me output as an object with key value pair as
{
message: "Hello from backend"
}
but it returns undefined
------------UPDATE------------------------------ here's my entire router code, for a comment asked for it.
const express = require('express')
const router = express.Router()
const{scriptArray} = require('../Scripts/main')
router.route('/')
.get((req, res) =>{
res.render('homepage')
})
router.route('/playground')
.get((req, res) =>{
res.render('playground', {value: JSON.stringify(scriptArray)})
})
.post((req,res)=>{
// console.log(req);
res.send({message: 'Hello from backend'})
})
module.exports = {
router,
}
You're not returning properly! Do this:
return res.json();