See node. js clearly (ii)

Source: Internet
Author: User
Tags gz file

Compare buffers
Copy buffer

Routing means that we have different ways of dealing with different URLs, such as handling/start business logic and dealing with/upload modules, and logic is
is inconsistent.

Node. JS Module System:
http://www.shouce.ren/api/view/a/3470

Code area:

/** * Compare buffers * Buf1.compare (BUF2);*/varBuffer1 =NewBuffer (' ABC ');varBuffer2 =NewBuffer (' ABCD ');varresult =Buffer1.compare (buffer2); Console.log (result);if(Result < 0) {Console.log (Buffer1+ ' comes before ' +buffer2);}Else if(Result = = 0) {Console.log (Buffer1+ ' is same as ' +buffer2);}Else if(Result > 0) {Console.log (Buffer1+ ' comes after ' +buffer2);}/** * Copy buffer*/varBuffer1 =NewBuffer (' ABC ');varBuffer2 =NewBuffer (3); buffer1.copy (buffer2); Console.log (' Buffer2: ' + buffer2.tostring ());//BUFFER2:ABC//Buffer ClippingvarBuffer1 =NewBuffer (' Runoob ');varBuffer2 = Buffer1.slice (0, 2); Console.log (' Buffer2 content: ' +buffer2);//buffer length//Buf.lengthvarBuffer =NewBuffer (' www.baidu.com '); Console.log (' Buffer length: ' + buffer.length ';//length of buffer:/** node. js Stream (Stream)*///reading data from the stream//Introducing File SystemsvarFS = require (' FS ');vardata = ';//to create a readable streamvarReadstream = Fs.createreadstream (' input.txt '));//Set EncodingReadstream.setencoding (' UTF8 ');//Handling Flow EventsReadstream.on (' Data ',function(chunk) {data+=Chunk;}); Readstream.on (' End ',function() {console.log (data);}); Readstream.on (' Error ',function(Err) {console.log (err.stack);}); Console.log (' Program execution is complete! ‘);//Write Stream//introducing File System modulesvarFS = require (' FS ');vardata = ' This is Baidu's website: www.baidu.com ';varCreateFile = ' Output.txt ';varWritestream =Fs.createwritestream (createFile);//writing data using UTF8 encodingWritestream.write (data, ' UTF8 '));//Mark file Endwritestream.end ();//File Write completeWritestream.on (' Finish ',function() {Console.log (' Write Complete ');});//Error PromptWritestream.on (' Error ',function(Err) {console.log (err.stack);}); Console.log (' Program execution is complete! ‘);//Pipe Flow//get data from one stream and pass it to another stream//read the contents of a file and write the contents to another content//Introducing File SystemsvarFS = require (' FS ');//Create a readable streamvarReadstream = Fs.createreadstream (' input.txt '));//Create a writable streamvarWritestream = Fs.createwritestream (' output.txt '));//Pipe Read and write operationsreadstream.pipe (Writestream); Console.log (' Program execution is complete! ‘);//Chained Flow//connect the output stream to another stream and create multiple flow action chains//introducing File System modulesvarFS = require (' FS ');//introducing a compression modulevarZlib = require (' zlib '));//to create a readable streamFs.createreadstream (' Input.txt '). Pipe (Zlib.creategzip ()). Pipe (Fs.createwritestream (' Input.txt.gz ')); Console.log (' File compression complete ');//Creategunzip ()//chained stream Decompression filevarFS = require ("FS");varZlib = require (' zlib '));//Unzip the input.txt.gz file to Input.txtFs.createreadstream (' input.txt.gz '). Pipe (Zlib.creategunzip ()). Pipe (Fs.createwritestream (' Input.txt ')); Console.log (The file is unzipped. ");//node. JS Module System//node. js functionfunctionSay (Word) {console.log (word);}functionexecute (F, v) {f (v);} Execute (say,' Some words ');//Incoming anonymous functionExecutefunction(Word) {console.log (word);},' Some words ');//function passing is how to get the HTTP server to workvarHTTP = require (' http '); Http.createserver (ONrequest). Listen (8080);functiononrequest (Request, Response) {Response.writehead ($, {' Content-type ': ' Text/plain '}); Response.Write (' Some words '); Response.End ();}//node. JS Routing//requires URL and QueryString modulevarHTTP = require (' http ');varurl = require (' URL ')); Http.createserver (ONrequest). Listen (8080);functiononrequest (Request, Response) {Response.writehead ($, {' Content-type ': ' Text/plain '}); Response.Write (' Some words '); Response.End ();}//node. JS Global Object Globals//__filename: The file name of the script that is currently executingConsole.log (__filename);//E:\demo\node\demo01.js//the directory where the current execution script residesConsole.log (__dirname);//E:\demo\node//global variable process, which is the property of the global object//Process is an object that describes the state of the current node. JS ProcessProcess.on (' exit ',function(code) {SetTimeout (function() {Console.log (' The code will never execute '); }, 0); Console.log (' Exit code: ' + code ';//0}); Console.log (' End of program execution ');//output to TerminalProcess.stdout.write (' Some words ' + ' \ n ');//read by parameterProcess.argv.forEach (function(val, index, array) {Console.log (index+ ': ' +val);});/** * 0:d:\node_install\node.exe * 1:e:\demo\node\demo01*///Get absolute pathConsole.log (Process.execpath);//D:\node_install\node.exe//Get platform InformationConsole.log (Process.platform);//Win32//Output current directoryConsole.log (PROCESS.CWD ());//E:\demo\node//Output Current VersionConsole.log (process.version);//Output Memory UsageConsole.log (Process.memoryusage ());

See node. js clearly (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.