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 ...
随机推荐
- delphi xe 的替代者 Lazarus
Lazarus的设计目标是应用Free Pascal,所以所有凡是Free Pascal能运行的平台,Lazarus都可以运行.最新版本能运行于Linux,Win32和Mac OS.整个界面的外观和操 ...
- fgt2eth Script
fgt2eth Script explanation_on_how_to_packet_capture_for_only_certain_TCP_flags_v2.txt Packet capture ...
- MT【199】映射的个数
(2018中科大自招)设$S=\{1,2,3,4,5\}$则满足$f(f(x))=x$的映射:$S \longrightarrow S$的个数____解答:由于$a\ne b$时必须满足$f(a)=b ...
- 自学Zabbix3.10.1.5-事件通知Notifications upon events-媒介类型Script
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix3.10.1.5-事件通知Notifications upon events-媒介 ...
- 【转】I²C总线上拉电阻阻值如何选择?
I2C总线为何需要上拉电阻? I2C(Inter-Intergrated Circuit)总线是微电子通信控制领域中常用的一种总线标准,具有接线少,控制方式简单,通信速率高等优点. I2C总线的内部结 ...
- 【转】spi测试自发自收(中断通信方式)
1.初始化spi时钟 void spiRccinit(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); RCC_APB2Peri ...
- angular的一次小错误
前台页面的错误: 在使用angular的时候,发现了标签等不能解析,忙了一个小时没找见错误在哪,最后才发现,原来ng-app,ng-controller等声明错了地方,声明在了div上,而不是在bod ...
- Git基本知识
一,安装 Ubuntu安装git:sudo apt-get install git-core Centos安装git:yum install git-core 二,配置身份---在提交代码时可以辨别身 ...
- C#生成和识别二维码
用到外部一个DLL文件(ThoughtWorks.QRCode.dll),看效果 生成截图 识别截图 生成二维码后右键菜单可以保存二维码图片,然后可以到识别模式下进行识别,当然生成后可以用手机扫描识别 ...
- [Spring] 学习Spring Boot之二:整合MyBatis并使用@Trasactional管理事务
一.配置及准备工作 1.在 Maven 的 pom 文件中新增以下依赖: <dependency> <groupId>mysql</groupId> <art ...