Node.js POST

  • Browser sendet über HTTP POST Daten zum Server (Text, JSON, Datei, etc.)
  • diese kommen in Stücken an (Stream)

Beispiel: JSON Daten zum Server senden

Codeconst http = require('http');

const hostname = '127.0.0.1'; // localhost
const port = 3000;

const server = http.createServer((request, response) => {
  if (request.method === 'POST') {
    let jsonString = '';
    request.on('data', (data) => {
      jsonString += data;
    });
    request.on('end', () => {
      console.log(JSON.parse(jsonString));
    });
  }
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
JavaScriptasync function sendJSONStringWithPOST(url, jsonString) {
  const response = await fetch(url, {
    method: 'post',
    body: jsonString,
  });
}

sendJSONStringWithPOST(
  'http://localhost:3000/',
  JSON.stringify({ test: "Dies ist ein Test" })
);