1 /**2 * Created by Administrator on 2016/8/3.3 */4 varHTTP = require ("http");5 //Node import file system module6 varFS = require ("FS");7 functionStart (req, res) {8Res.writehead ($, {"Content-type": "Text/plain"});9Res.write ("Asynchronous read file and synchronous read file comparison!") ");TenRes.end ("Over"); One } A //create an HTTP server and listen for the port number 3001,host to 127.1.1.1 - varServer = Http.createserver (start). Listen ("3001", "127.1.1.1",function(){ - varPort =server.address (). Port; the varHost =server.address (). Address; -Console.log ("Application instance, Access address is http://%s:%s", host, port); - }); - //asynchronous read [non-blocking] +Console.log ("Asynchronous Start:"); - //function to read the contents of a file asynchronously: ReadFile () +Fs.readfile ("Index.txt", "Utf-8",function(err, data) { A if(err) { at Console.log (err); -}Else{ - console.log (data); - } - }); -Console.log ("End Async"); in //synchronous read [block, code execution after blocking] -Console.log ("Sync Start:"); to //functions to read the contents of a file synchronously: Readfilesync () + vardata = Fs.readfilesync ("Index.txt", "Utf-8"); - console.log (data); theConsole.log ("End Sync");
Asynchronous synchronous read File content comparison