var url=require('url')
var qs=require('querystring')
http.createServer(onRequest).listen(3000)
function onRequest(res,resp)
//第一种方式queryString模块的方式获取参数的方法
var arg=url.parse(res.url).query ;
var nameValue=qs.parse(arg)['name']
console.log('name:',nameValue)
//利用Url模块的方式获取参数的方法
var arg2=url.parse(res.url,true).query
console.log('age:'arg2.age)
resp.writeHead(200)
resp.write('Hellow Node.js')
resp.end()
如何处理请求的路由:
Node.js中处理路由的方法需要利用request.ur获取客户端的请求判断
* Created by longlingxiu on 15/2/10.
var http = require( 'http' )
var handlePaths = []
* 初始化路由配置数组
function initRotute()
handlePaths.push( '/' )
handlePaths.push( '/login' )
handlePaths.push( '/register' )
* 处理路由的逻辑
* @param path
function rotuteHandle( path )
// 遍历路由配置信息
for ( var i in handlePaths )
if( handlePaths[i] == path )
console.log( '获取到相同的路由信息:',handlePaths[i] )
var rlt = "request rotute is:" + handlePaths[i]
return rlt
return '404 Not Found'
* 服务器回掉函数
* @param request
* @param response
function onRequest( request, response )
var requestPath = request.url
console.log('请求的路径是=>',requestPath )
response.writeHead( 200, {
'Content-Type':'text/plain'
var responseContent = rotuteHandle( requestPath )
response.write( responseContent )
response.end()
var server = http.createServer( onRequest )
server.listen( 3000 )
initRotute()
console.log('Server is Listening right now ..')
判断post 和get
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...' )
<!DOCTYPE html>
<meta charset="UTF-8">
<title></title>
</head>
<form method ="post" action="http://localhost:3000/login" >
<textarea name="text"></textarea>
<input type='submit' value="commit" />
</form>
</body>
</html>
文件上传:
var formidable=require('formidable')
var http=require('http')
var sys = require('sys');
http.createServer(function ( request, response )
if( request.url == '/upload' && request.method.toLowerCase() == 'post' )
console.log( 'upload requet ' )
uploadRequest(request,response);
return;
enterRequest(request,response)
}).listen(3000)
function enterRequest( request, response )
response.writeHead( 200, { 'Content-type' : 'text/html' });
response.end(
'<form action = "/upload" enctype="multipart/form-data" method="post" >' +
'<input type = "text" name = "title" /> <br>' +
'<input type = "file" name="upload" multiple="multiple"/> <br/>'+
'<input type="submit" value="Upload Now"/>' +
'</form>'
function uploadRequest( request,response )
var form = new formidable.IncomingForm();
form.parse( request, function ( err, fields, files ) {
response.writeHead(200, {'Content-type' : 'text/plain'});
response.write('reviced upload file');
response.end( sys.inspect(
fields : fields,
files : files
}) );