Basic concepts of Node.js and processing of reading and writing files

1. Basic concepts: 1. B
/S programming model:
*Browser-Server
*back-end
*The BS programming model of all server technologies is the same, independent of language
*Node is just a programming model tool
2. Modular programming
*RequireJS
*SeaJS
*@import('file path')
*JavaScript can generally only be loaded with the tag script
*In Node, the script file that loads JS can be referenced like @import()

3. Node commonly used API
4. Asynchronous programming:
*Callback function
*Promise
*async
*generator
5.Express development framework
6.Ecmascript 6
2. 1. In node, use EcmaScript for coding
2. No BOM, DOM
3. And browse The JavaScript in the browser is different. The js in the browser does not have file operation capabilities, but the javascript
in node has file operation capabilities . 4. In node, if file operations are performed, the core module fs must be introduced, which provides all APIs related to file operations,
for example: fs.readFile() // means to read files

// 使用require方法加载fs (fileSystem)核心模块
var vm = require('fs');
//读取文件:
    // 第一个参数是文件的读取路劲,第二个参数是回调函数
    /*如果成功:
         data 返回数据
         error  null
      如果回调失败:
         data null
         error 错误对象
    */
vm.readFile('./vmOne.txt', function(error,data){
    
    
     //将二进制或者十六进制转化为字符串
     console.log(data.toString());
});
/*
    第一个参数:"文件路径",
    第二个参数:"文件内容"
    第三个参数:"回调函数"
        error:文件写入成功,error是null,文件写入失败,error是错误对象
*/
vm.writeFile('./text/note2.txt','年少的轻狂不能用来挥霍',function(error){
    
    
     if(error === null ){
    
    
          console.log('Write success!');
     }else{
    
    
          console.log('Write Failure!');
     }
});

Guess you like

Origin blog.csdn.net/Vodka688/article/details/114836366