Asynchronous synchronous read File content comparison

Source: Internet
Author: User
Tags readfile

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

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.