Server将表单数据获取并返给Client

来源:互联网 发布:apache zip jar 编辑:程序博客网 时间:2024/05/12 03:24
var http =require('http');var querystring = require('querystring');http.createServer(function(req, res){    switch (req.url) {        case '/form':            if (req.method == 'POST') {                console.log('[200] ' + req.method + " to" + req.url);                var fullBody = '';                req.on('data', function(chunk){                    fullBody += chunk.toString();                });                req.on('end', function(){                    res.writeHead(200, 'OK', {'Content-Type':'text/html'});                    res.write('<html><head><title>Post Data</title></head><body>');                    var bodytt = querystring.parse(fullBody);                    for(var prop in bodytt) {                        res.write('<h5>' + prop + ':' + bodytt[prop] + '</h5>');                    }                    res.write('</body></html>');                    res.end();                });            } else {                console.log('[405] ' + req.method + ' to ' + req.url);                res.writeHead(405, "Method not supported", {'Content-Type':'text/html'});                res.end('<html><head><title>error 405</title></head><body>Method is not supported</body></html>');            }            break;        case '/index':        default :            res.writeHead(404, "Not Found Page", {'Content-Type':'text/html'});            res.end('<html><head><title>404 error</title></head><body>Page Is Not Found</body></html>');            console.log('[404] ' + req.method + " to " + req.url);    }}).listen(8080, '127.0.0.1');
1 0
原创粉丝点击