Handling non-blocking I/O/** Method of callback function Async*//*function f (CB) {fs.readfile ('./4 ', (Err,data) =>{CB (Data.tostring ());//callback})}f (function (data) {Console.log ( (data);});*/functionf () {Fs.readfile ('./4 ', (err,data) ={myevent.emit (' Talk ', data.tostring ());//event to bind})}//Performing----Publishersf (); Myevent.on (' Talk ', (data) ={console.log (data);//data = = data.tostring ()}) Three URL module and QueryString module;varqueryString = require (' quirestring '));//query string,//Querystring.parse (String) parses a string into an object;Let str = ' Name:idhorse?age:18?gender:male '; let obj= Querystring.parse (str, '? ', ': '))//The second parameter is & by default, and the third one is =//Parse Result {name: ' Idhorse ', Age: ' + ', Gender: ' Male '};//querystring.stringify () parses an object into a stringLet str2 = querystring.stringify (obj, ' & ', ' = ');//' Name=idhorse&age=18&gender=male 'Let a= encodeURIComponent (str)//encode and encrypt the processingLet B = decodeuricomponent (a);//for decoding processingparsing URLsvarHTTP = 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 (' The request is a.html, the requested parameter is ')}} response.end (' Get the request! ‘);}). Listen (, () ={Console.log (' Start ')})//====parsing URLsvarHTTP = 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 (' The request is a.html, the requested parameter is ' +json.stringify (OBJ)); }} response.end (' Get the request! ‘);}). Listen (3000);//=======Processing Post RequestsvarHTTP = require (' http ');varServer = 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 (' Get the request! ‘); Res.end ();}). Listen (3000);//project file upload formidableLet 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)Throwerr; 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)Throwerr; Console.log (EXT); Res.writehead (200,{' content-type ': ' Text/plain;charset=utf-8 '}); Res.write (Success ‘); Res.end (); })})}) Server.listen (, () ={Console.log (' Start '); }) three. HTTP Module 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 handling URLs Common methods