node.js处理post请求

1.html

node.js处理post请求

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  postData = ""
        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...' )

  

上一篇:iOS 中 const static extern 关键字总结


下一篇:第一篇帖子,就弄个JS动态公告浏览吧,直接上代码