Похожие чаты

Hey everone, got a question about node.js and they way

its events work. I've written this and I expect it to return my request body as the response, but it gives me this error unncaught (in promise) SyntaxError: Unexpected end of JSON input
but when I put the req.pipe(res) line outside of the event listener block it works as expected, why is that?

const server = http.createServer((req, res) => {
const items = req.url.split('/');
if (req.method === 'POST' && items[1] === 'friends') {
req.on("data", (data) => {
console.log("data")
const friend = data.toString();
firnds.push(JSON.parse(friend));
req.pipe(res)
})
}

2 ответов

29 просмотров
elo- Автор вопроса

my realization is that the request stream comes in chunks and it might contain multiple chunks of data, and the reason for this error is that the req.pipe(res) line gets executed before the entire request is processed. but as you see I've added a console.log("data") to log whenever a chunk of data arrives, and it only happens once. doesn't that mean that the entire request body is passed in one chunk and it should work ok? again, the code works well when I put the req.pipe(res) line outside of the event listener block.

That's not going to work

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта