码迷,mamicode.com
首页 > 其他好文 > 详细

node-表单验证

时间:2016-02-12 15:06:37      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

var http = require(‘http‘);
var url = require(‘url‘);
var fs = require(‘fs‘);
var querystring = require(‘querystring‘);

var server = http.createServer();

var HtmlDir = __dirname + ‘/html/‘;

server.on(‘request‘, function( req, res ) {

    var urlStr = url.parse( req.url );

    switch (urlStr.pathname) {
        case ‘/‘:
            //首页
            sendData( HtmlDir + ‘index.html‘, req, res );
            break;

        case ‘/user‘:
            //用户首页
            sendData( HtmlDir + ‘user.html‘, req, res );
            break;

        case ‘/login‘:
            //用户登录
            sendData( HtmlDir + ‘login.html‘, req, res );
            break;

        case ‘/login/check‘:
            //console.log(req.method);
            //console.log(urlStr);

            //console.log( querystring.parse(urlStr.query) );

            if (req.method.toUpperCase() == ‘POST‘) {

                var str = ‘‘;

                req.on(‘data‘, function(chunk) {
                    str += chunk;
                })

                req.on(‘end‘, function() {
                    console.log(str);
                    console.log( querystring.parse( str ) );
                })

            }

            break;

        default:
            //处理其他情况
            sendData( HtmlDir + ‘err.html‘, req, res );
            break;
    }

});

function sendData(file, req, res) {
    fs.readFile( file, function(err, data) {

        if (err) {
            res.writeHead(404, {
                ‘content-type‘ : ‘text/html;charset=utf-8‘
            });
            res.end(‘<h1>页面被LEO吃掉了</h1>‘);
        } else {
            res.writeHead(200, {
                ‘content-type‘ : ‘text/html;charset=utf-8‘
            });
            res.end(data);
        }

    } );
}

server.listen(8080, ‘localhost‘);

 

node-表单验证

标签:

原文地址:http://www.cnblogs.com/mayufo/p/5187040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!