NodeJS之http数据解析
来源:互联网 发布:java垃圾回收优点 编辑:程序博客网 时间:2024/06/06 17:19
数据请求
前台:form , ajax , jsonp
后台:都一样
无论前后台如何,都是通过http协议 , 前台 <-> 后台
请求方式
GET,数据在url
POST,数据不在url
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title></title> <style type="text/css"></style></head><body> <form action="http://localhost:8080/mango" method="get"> uName: <input type="text" name="user" value=""> <br> passwd: <input type="password" name="psd" value=""> <br> <input type="submit" value="submit"> </form></body></html>
const http = require('http');var server = http.createServer(function(req, res) { //req:获取前台过来的请求 console.log(req.url);// req.url = '/mango?user=guoyu&password=123456' // /mango?user=guoyu&psd=123456 var GET = {}; if (req.url.indexOf('?') != -1) {//chrome经常请求favicon.ico而且不带?所以要把这个废物排出 var arr = req.url.split('?'); var url = arr[0];//地址 '/mango' var arr2 = arr[1].split('&'); for (var i = 0; i < arr2.length; i++) { var arr3 = arr2[i].split('='); //arr3[0] => 名字 'user' //arr3[1] => 数据 'guoyu' GET[arr3[0]] = arr3[1]; } } else { var url = req.url; } console.log(url, GET);// /mango { user: 'guoyu', psd: '123456' }// /favicon.ico// /favicon.ico {} res.write('abc123'); res.end();});server.listen(8080);
上面的代码过于繁杂,可以直接引用一个模块,简化代码
const http = require('http');var querystring = require('querystring');// var json = querystring.parse('user=blue&pass=123456&age=18');//直接返回一个json// console.log(json);// { user: 'blue', pass: '123456', age: '18' }var server = http.createServer(function(req, res) { //req:获取前台过来的请求 console.log(req.url);// req.url = '/mango?user=guoyu&password=123456' var GET = {}; if (req.url.indexOf('?') != -1) { var arr = req.url.split('?'); var url = arr[0]; GET = querystring.parse(arr[1]); } else { var url = req.url; } console.log(url, GET);// /mango { user: 'guoyu', psd: '123456' }// /favicon.ico// /favicon.ico {} res.write('abc123'); res.end();});server.listen(8080);
其实还有更简单的
const urlLib = require('url');
const urlLib = require('url');//true:是否解析query属性var obj = urlLib.parse('http://www.baidu.com/index.html?uname=guoyu&age=27&sex=male',true);console.log(obj);Url { protocol: 'http:', slashes: true, auth: null, host: 'www.baidu.com', port: null, hostname: 'www.baidu.com', hash: null, search: '?uname=guoyu&age=27&sex=male', query: { uname: 'guoyu', age: '27', sex: 'male' }, pathname: '/index.html', path: '/index.html?uname=guoyu&age=27&sex=male', href: 'http://www.baidu.com/index.html?uname=guoyu&age=27&sex=male'}var obj1 = urlLib.parse('http://www.baidu.com/index.html?uname=guoyu&age=27&sex=male');console.log(obj1);Url { protocol: 'http:', slashes: true, auth: null, host: 'www.baidu.com', port: null, hostname: 'www.baidu.com', hash: null, search: '?uname=guoyu&age=27&sex=male', query: 'uname=guoyu&age=27&sex=male', pathname: '/index.html', path: '/index.html?uname=guoyu&age=27&sex=male', href: 'http://www.baidu.com/index.html?uname=guoyu&age=27&sex=male' }
const http = require('http');const querystring = require('querystring');const urlLib = require('url');var server = http.createServer(function(req, res) { console.log(req.url); var obj = urlLib.parse(req.url, true); var url = obj.pathname; var GET = obj.query; console.log(url, GET);// /mango { user: 'guoyu', psd: '123456' }// /favicon.ico// /favicon.ico {} res.write('abc123'); res.end();});server.listen(8080);
阅读全文
0 0
- NodeJS之http数据解析
- nodejs API之HTTP
- HTTP数据包头解析---之温故而知新!
- 用nodejs解析json数据
- nodejs-http 对form表单上传文件数据的解析过程
- 大话NodeJS之------Http模块
- 02-nodejs之http模块
- Nodejs源码解析之events
- Nodejs源码解析之module
- Nodejs源码解析之Buffer
- Nodejs源码解析之util
- Nodejs源码解析之module
- nodejs获取http请求的传入数据
- HTTP数据包头解析
- HTTP数据包头解析
- HTTP 数据包头解析
- HTTP 数据包头解析
- nodejs之处理表单数据
- Linux 驱动中断处理
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- mysql 集群架构
- 配置Visual Studio 2015+OpenGL可运行蓝宝书源码
- NodeJS之http数据解析
- Linux多版本gcc共存
- 使用window下的库
- 【个人笔记重点,不作为参考】主题:待看的angular2课程
- eclipse导入svn项目之后java文件图标空心问题的解决
- 音视频记录和转换工具ffmpeg编译安装说明
- python终端中执行py文件
- dubbo redis
- JVM系列二:GC策略&内存申请、对象衰老