Nodejs module

A, fs module

 (1) Viewing File Status

  fs.stat(path, callback) / statSync(path)

 (2) create the directory

  fs.mkdir(path, callback) / mkdirSync(path)

 (3) Remove directory

  fs.rmdir(path, callback) / rmdirSync(path)

  Using asynchronous remove the directory mydir

  Use directory synchronization to remove mydir2

 (4) read directory

  fs.readdir(path, callback) / readdirSync(path)

    callback

       Err error may occur  

       Results result read array format

 (5) Create Files / Write File

  fs.writeFile( path, data, callback )

     / writeFileSync(path,data)

     Data to be written to the data

  If the file does not exist, create a file and write data;

  If the file already exists, it will empty the contents of the file, and then write data

 (6) additional write file

  fs.appendFile(path, data, callback) /

    appendFileSync(path, data)

  If the file does not exist, create a file, and then write the data

  If data is written at the end of the file already exists, it will re-file

 Exercise: create an array, each element is a program name, through the array, respectively, to obtain elements of the data using the synchronization method is written to the file data.txt

 (7) to read the file

   fs.readFile(path, callback) / readFileSync(path)

     callback

        err

        Data read data, the format buffer

 (8) Delete File

   fs.unlink(path, callback) / unlinkSync(path)

 If (9) is determined file exists

   fs.existsSync(path)

  练习:判断data.txt是否存在,如果存在,则删除

 (10)文件流

   var readStream=fs.createReadStream(文件路径)  //创建可读流

   readStream.on('data', function(chunk){   })

    当有数据流入,自动触发事件;

    通过回调函数来获取 ,chunk就是获取的数据流

   readStream.on('end', function(){     })

    当读取结束,自动触发事件

   pipe  管道,可以将数据从可读流添加到可写流

 

2.http协议

 浏览器和web服务器之间的通信协议

 (1)通用头信息

   Request URL: 请求的URL,对应浏览器地址栏;向服务器获取哪些内容

   Request Method: 请求的方法,获取内容的方法 get/post

   Status Code: 响应的状态码

     1**:正在响应,还没有结束

     2**:成功的响应

     3**:响应的重定向,跳转到另一个URL

     4**:客户端请求错误

     5**:服务器端错误

   Remote Address:请求的服务器的IP地址及端口号

 (2)响应头信息

   Connection: 连接方式,keep-alive 持久连接

   Content-Encoding: 内容压缩形式, gzip

   Content-Type: 响应的文件类型

   Location: 跳转的URL,常配合着状态码3**使用

 (3)请求头信息

   Accept: 浏览器接收的文件类型有哪些

   Accept-Encoding: 接收的压缩形式有哪些

   User-Agent: 客户端使用的浏览器版本

 (4)请求主体

   可有可无,浏览器向服务器发请求传递的数据

3.http模块

 模拟浏览器向web服务器发请求,还可以创建web服务器

 (1)模拟浏览器

  http.get(url, callback)  发送请求

    url  请求的URL 

    callback  回调函数,获取服务器端的响应

      res  响应的对象

      res.statusCode  获取响应的状态码

      res.on('data', function(chunk){   })

       事件: 监听服务器端是否有数据传输过来

       chunk  就是传输的数据,格式为buffer

 (2)创建web服务器

var app=http.createServer(); //创建web服务器

app.listen(8080);//设置监听的端口

//监听浏览器的请求

app.on('request', function(req,res){

  req  请求的对象

    req.url  请求的url,获取端口号后边的部分

    req.method  请求的方法

    req.headers  请求的头信息

  res  响应的对象

    res.write()  设置响应的内容

    res.writeHead( 状态码, 头信息对象 )

    res.end()  结束并发送响应到浏览器

})

Guess you like

Origin www.cnblogs.com/sna-ling/p/12514233.html