leafsummer
7/8/2015 - 3:26 AM

nodejs, parse post request (simple example)

nodejs, parse post request (simple example)

var http = require('http');
var querystring = require('querystring');

var server = http.createServer(function(req, res){
  var post = '';
  
  req.on('data', function(chunk){
    post += chunk;
  });
  
  req.on('end', function(){
    post = querystring.parse(post);
    res.write(post.title);
    res.write(post.text);
    res.end();
  });
}).listen(3000);

//use the express

var express = require('express');

var app = express.createServer();
app.use(express.bodyParser());
app.all('/', function(req, res){
  res.send(req.body.title + req.body.text);
});

app.listen(3000);