1. http/ querystring만 사용
- var http = require('http')
- ,qs = require('querystring');
- http.createServer(function (req, res) {
- if( req.url == '/' && req.method == 'POST'){
- var postBody = '';
- req.on('data', function (data) {
- postBody += data;
- });
- req.on('end', function () {
- var post = qs.parse(postBody);
- //post데이터확인
- console.log(post['postname']);
- });
- res.end('true');
- }else{
- res.writeHead(404, {'Content-Type': 'text/plain'});
- res.end('404 ERROR');
- }
- }).listen(8001);
2. http / express / body-parser 사용
- var express = require('express')
- ,http = require('http')
- ,bodyParser = require('body-parser');
- var app = express();
- app.use(bodyParser.urlencoded({extended: true}));
- //app.use(bodyParser.json());
- app.post("/", function( req, res){
- res.writeHead(200,{"Content-Type" : "text/plain"});
- //post데이터확인
- console.log(req.param('postname',null));
- res.end('test');
- });
- app.use(function( req, res){
- res.writeHead(404,{"Content-Type" : "text/plain"});
- res.end('404 ERROR');
- });
- http.createServer(app).listen(8001);