首页 > 代码库 > node.js处理post请求

node.js处理post请求

1.html

技术分享

 

2.app.js

var http = require(‘http‘)
var qs =  require(‘querystring‘)
/**
 * 路由控制的功能
 * @param path
 */
function rotuteHandle( request )
{
    if( request.url == ‘/login‘ && request.method.toLowerCase() == ‘post‘ )
    {
        console.log(‘获取login的post请求‘)
        return ‘post method‘
    }
    return ‘get method‘
}

/**
 * Server 回掉
 * @param request
 * @param response
 */
function onRequest(request,response)
{
    request.setEncoding(‘utf-8‘)
    response.writeHead(200,{
        ‘Content-Type‘:‘text/plain‘
    })

    if(request.url == ‘/login‘ && request.method.toLowerCase() == ‘post‘){
        var  postDatahttp://www.mamicode.com/= ""
        request.addListener(‘data‘, function (data) {
            console.log(‘获取post请求参数中..‘)
            postData += data
        })
        request.addListener(‘end‘, function () {
            console.log(‘获取post参数成功‘)
            console.log( postData )
            var content =  qs.parse(postData).text
            response.write( content )
            response.end()
        })
    }else{
        response.write( ‘other method‘ )
        response.end()
    }
}

var server = http.createServer( onRequest )
server.listen( 3000 )
console.log( ‘Server is Listening...‘ )

  

node.js处理post请求