node.js处理url常用方法
Posted 悔创阿里-杰克马
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了node.js处理url常用方法相关的知识,希望对你有一定的参考价值。
处理非阻塞I/O /* *回调函数的方法 异步 */ /* function f(cb){ fs.readFile(‘./4‘,(err,data)=>{ cb(data.toString()); //回调 }) } f(function(data){ console.log(data); }); */ function f(){ fs.readFile(‘./4‘,(err,data)=>{ myEvent.emit(‘talk‘,data.toString());//事件进行绑定 }) } //执行 ----发布者 f(); myEvent.on(‘talk‘,(data)=>{ console.log(data); //data == data.toString() }) 三URL模块 和queryString模块; var queryString = require(‘quirestring‘); //查询字符串, //queryString.parse(string)将字符串解析成对象; let str = ‘name:idhorse?age:18?gender:male‘; let obj = queryString.parse(str,‘?‘,‘:‘) //默认情况下 第二个参数是&,第三个是= //解析结果 {name:‘idhorse‘,age:‘18‘,gender:‘male‘}; //queryString.stringify()将一个对象解析成一个字符串 let str2 = queryString.stringify(obj,‘&‘,‘=‘); //‘name=idhorse&age=18&gender=male‘ let a = encodeURIComponent(str)//进行编码加密处理 let b = decodeURIComponent(a);//进行解码处理 解析url var http = require(‘http‘), urlApi = require(‘url‘), server; server = http.createSever((request,response)=>{ response.writeHead(200,{‘Content-type‘:‘text/plain;charset=UTF-8‘}); if(request.url!=‘/favicon.ico‘){ let url = urlApi.parse(request.url,true); cosoleo.log(url); if(url.pathname==‘/a.html‘){ res.end(‘这里请求的是a.html,请求的参数是‘) } } response.end(‘收到请求!‘); }).listen(3000,()=>{ console.log(‘start‘) }) //==== 解析url var http = require(‘http‘), server; sever = http.createServer(function(request,response){ response.writeHead(200,{‘Content-type‘:‘text/plain;charset=UTF-8‘}); let url = request.url; if(url!= ‘/favicon.ico‘){ let arr1 = url.split(‘?‘); let arr2 = arr1[1].split(‘&‘); let Obj = {}; arr2.map(function(data,i){ Obj[data.split(‘=‘)[0]]=data.split(‘=‘)[1]; }) if(arr1[0]==‘/a.html‘){ response.end(‘这里请求的是a.html,请求的参数是‘+JSON.stringify(Obj)); } } response.end(‘收到请求!‘); }).listen(3000); //======= 处理post请求 var http = require(‘http‘); var server = http.createServer((req,res)=>{ res.writeHead(200,{‘Content-type‘:‘text/plain;charset=UTF-8‘}); console.log(‘req,url‘); let str = ‘‘; res.on(data,(data)=>{ ‘use strict‘; str+= data; }); res.on(‘end‘,()=>{ console.log(str); }); res.write(‘收到请求!‘); res.end(); }).listen(3000); // 项目文件上传 formidable let http = require(‘http‘); let urlLib = require(‘url‘); let fs = require(‘fs‘); let pathLib = require(‘path‘); let formidable = reqiure(‘formidable‘); let server = http.createServer((req,res)=>{ let urlObj = urlLib.parse(req.url,true); if(urlObj.pathname===‘/‘){ res.readFile(‘./template/form.html‘,(err,data)=>{ res.write(data); res.end(); }) } if(urlObj===‘/upload‘){ let form = formidable.IncomingForm(); form.encoding = ‘utf-8‘; form.upLoadDir = _dirname+‘/folder‘; form.parse(req,function(err,fields,files){ if(err)throw err; console.log(files); let ext = pathLib.exname(files.wangpengde.name); let oldPath = files.wangpengde.path; let newPath = pathLib.join(_dirname,‘/folder/‘)+files.wangpengde.name; fs.rename(oldPath,newPath,(err)=>{ if(err)throw err; console.log(ext); res.writeHead(200,{‘content-type‘:‘text/plain;charset=UTF-8‘}); res.write(‘成功!‘); res.end(); }) }) } }) server.listen(3000,()=>{ console.log(‘start‘); }) 三. http模块 const http = require(‘http‘); const cheerio = require(‘cheerio‘); http.get(‘http://www.easyvoa.com‘,function(res){ if(res.statusCode ==200){ let str = ‘‘; res.on(‘data‘,(data)=>{ str += data; }); res.on(‘end‘,()=>{ const $ = cheerio.load(str); const titles = $(‘.title_a‘); for(let i = 0; i<titles.length ;i++){ console.log(titles.eq(i).text()); } }) } })
以上是关于node.js处理url常用方法的主要内容,如果未能解决你的问题,请参考以下文章
Node.js GET/POST对应的url/query-string常用的方法介绍