9951 explained code solutions for 126 technologies


nodejsHow to get post data variables in HTTP server


const http = require('http');
const qs = require('querystring');

const server = http.createServer((req, res) => {
  let data = '';
  req.on('data', chunk => {
    data += chunk;
  });

  req.on('end', () => {
    let post = qs.parse(data);
    console.log(post)
    res.end();
  });
});

server.listen(82, '127.0.0.1');ctrl + c
require('http')

import module to work with http protocol

http.createServer

creates HTTP server

req.on(

handle specific http event

on('data'

handle data receiving with specified callback

data += chunk

collect all post data chunks into single data variable

on('end'

handle event when request has finished

server.listen

launch server on a given port and hostname

qs.parse

parse given string as url encoded variables

let post

will contain parsed variables