moringaman
5/5/2019 - 8:24 PM

Reading Streamed Data into a buffer with Nodejs

  if (url === '/message' && method === 'POST') {
    const body = [];
    req.on('data', chunk => {
      body.push(chunk);
    });
    req.on('end', () => {
      const parsedBody = Buffer.concat(body).toString();
      let message = parsedBody.split('=')[1];
      console.log(message);
      fs.writeFileSync('messages.txt', message);
      res.statusCode = 302;
      res.setHeader('location', '/');
      return res.end();
    });
  }