node.js获取请求参数的方法和文件上传
var http=require('http')
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>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<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
}) );
})
}
node.js获取请求参数的方法和文件上传的更多相关文章
- js 禁止表单提交的方法(文件上传)
添加图片上传的表单,在form 添加属性onsubmit,提交之前会执行其中的方法,若返回FALSE,不会提交,返回TRUE,才会提交 <form method="post" ...
- (转)用JS获取地址栏参数的方法(超级简单)
转自http://www.cnblogs.com/fishtreeyu/archive/2011/02/27/1966178.html 用JS获取地址栏参数的方法(超级简单) 方法一:采用正则表达式获 ...
- js获取url参数的方法
js获取url参数的方法有很多. 1.正则分析 function getQueryString(name) { var reg = new RegExp("(^|&)" + ...
- springMvc源码学习之:spirngMVC获取请求参数的方法2
@RequestParam,你一定见过:@PathVariable,你肯定也知道:@QueryParam,你怎么会不晓得?!还有你熟悉的他 (@CookieValue)!她(@ModelAndView ...
- struts2获取请求参数的三种方式及传递给JSP参数的方式
接上一篇文章 package test; import com.opensymphony.xwork2.ActionSupport; import javax.servlet.http.*; impo ...
- node.js+express+jade系列六:图片的上传
安装npm install formidable 先把文件上传到临时文件夹,再通过fs重命名移动到指定的目录即可 fs.rename即重命名,但是fs.rename不能夸磁盘移动文件,所以我们需要指定 ...
- SpringMVC框架笔记02_参数绑定返回值文件上传异常处理器JSON数据交互_拦截器
目录 第1章 高级参数的绑定 1.1 参数的分类 1.2 数组类型的参数的绑定 1.3 集合类型的参数的绑定 第2章 @RequestMapping的用法 2.1 URL路径映射 2.2 请求方法限定 ...
- 鸿蒙的js开发部模式18:鸿蒙的文件上传到python服务器端
1.首先鸿蒙的js文件上传,设置目录路径为: 构建路径在工程主目录下: 该目录的说明见下面描述: 视图构建如下: 界面代码: <div class="container"&g ...
- resumable.js —— 基于 HTML 5 File API 的文件上传组件 支持续传后台c#实现
在git上提供了java.nodejs.c#后台服务方式:在这里我要用c#作为后台服务:地址请见:https://github.com/23/resumable.js 我现在visual studio ...
随机推荐
- DBA 这个角色
下面这些领域的技能可以提升DBA团队对公司业务产生正面影响的重要能力: ---------------------------------------------------------------- ...
- ef 更新数据库
//一:数据库不存在时重新创建数据库 Database.SetInitializer<testContext>(new CreateDatabaseIfNotExists<testC ...
- MT【76】直线系
解答 :答案是3,4.
- 【刷题】LOJ 6122 「网络流 24 题」航空路线问题
题目描述 给定一张航空图,图中顶点代表城市,边代表两个城市间的直通航线.现要求找出一条满足下述限制条件的且途经城市最多的旅行路线. 从最西端城市出发,单向从西向东途经若干城市到达最东端城市,然后再单向 ...
- emWin notes
emwin 学习小记 @2018-7-5 ## 在使用 graph 控件时,需要在坐标上显示波形,波形刷新函数 GRAPH_DATA_YT_AddValue() 放在一个 while(1) 中,这样便 ...
- Aop学习笔记
在学习编程这段时间我想大家都是习惯了面向过程或者面向对象的思想来编程,较少或者没有接触过面向方面编程的思想. 那么什么是面向方面(Aspect)——其实就是与核心业务处理逻辑无关的切面,例如记录日志. ...
- ubuntu系统问题解决集
1.解决ubuntu 14 system setttings失效的问题 sudo apt-get install unity-control-center 2. 支持root用户登录 修改以下配置文件 ...
- Linux下防御ddos攻击
导读 Linux服务器在运营过程中可能会受到黑客攻击,常见的攻击方式有SYN,DDOS等.通过更换IP,查找被攻击的站点可能避开攻击,但是中断服务的时间比较长.比较彻底的解决方法是添置硬件防火墙.不过 ...
- JavaScript--Dom间接选择器
一.Dom间接选择器 间接查找的属性 parentNode // 父节点 childNodes // 所有子节点 firstChild // 第一个子节点 lastChild // 最后一个子节点 n ...
- CentOS7安装新版git
#安装Git yum install -y epel-release rpm -ivh https://centos7.iuscommunity.org/ius-release.rpm yum lis ...