[node.js] [1] initial node.js

initial node.js


Node.js 是一个开源和跨平台的 JavaScript 运行时环境。


以下是本篇文章正文内容,下面案例可供参考

1. What is node.js

  • Node.js applications run in a single process without creating a new thread for each request.
  • Node.js provides a set of asynchronous I/O primitives in its standard library to prevent JavaScript code from blocking. Typically, libraries in Node.js are written using a non-blocking paradigm, making blocking behavior the exception rather than the norm . This allows Node.js to handle thousands of concurrent connections using a single server without introducing the burden of managing thread concurrency (which can be a significant source of bugs).
  • Node.js has a unique advantage: you can write server-side code in addition to client-side code. In Node.js, the new ECMAScript standard can be used without problems, because instead of having to wait for all users to update their browsers, decide which ECMAScript version to use by changing the Node.js version, and by running Node with the flag .js to enable certain experimental features.

Differences from browser scripts

  • Both browsers and Node.js use JavaScript as their programming language. Building an application that runs in the browser is quite different from building a Node.js application. While it's always JavaScript, there are a few key differences that make the experience quite different. Node.js applications bring a huge advantage: the comfort of writing everything (frontend and backend) in one language.
  • There is no document, window, and all the other objects that browsers provide in Node.js. In the browser, we don't have all the friendly APIs that Node.js provides through its modules, like filesystem access.
  • The environment can be controlled in Node.js. Since JavaScript evolves so fast, but browsers can be a bit slow to upgrade, sometimes on the web you're stuck with old JavaScript/ECMAScript versions. You can use Babel to transpile your code to be ES5 compliant before publishing it to the browser, but in Node.js, you don't need it.

2. Use node.js script

Import txt text and judge whether it is read successfully

//导入fs模块,来操作文件

const fs = require('fs');

//调用 fs.readFile()方法读取文件
//     参数1:读取文件的存放路径
//     参数2:读取文件时候采用的编码格式
//     参数3:回调函数,拿到读取失败和成功的结果  err dataStr   

fs.readFile('./1读取文本111.txt','utf8',function(err,dataStr){
    
    
   if(err){
    
    
        //message 属性用于设置或返回错误信息
        return console.log('文件读取失败 '+ err.message);
    }else{
    
    
        console.log('文件读取成功:内容是' + result);
    }
})

err is used to check errors
dataStr is the printed txt string data

Use the terminal powerShell to check whether the file is successfully read
insert image description here
and write the file

const fs = require('fs');
//调用fs.writeFile()方法,写入文件内容
  //参数1:表示文件的存放路径
  //参数2:表示要写入的内容
  //参数3:编码格式(可选项)
  //参数4:回调函数
fs.writeFile('./4写入文件内容','forever',function(err){
    
    
    if(err){
    
    
        return console.log('文件写入失败'+err.message);
    }else{
    
    
        console.log('文件写入成功');
    }
})
//当两次写入在同一个文件时,文件里的第一次写入的内容会被第二次覆盖掉

insert image description here

Summarize

Node.js enables js to be applied to the backend.
Because I need to import the student grade file .txt today, I have come into contact with node.js. If there are any mistakes, please point them out. Next time, I will introduce the processing of data in the txt file in node.js

おすすめ

転載: blog.csdn.net/weixin_51612770/article/details/124933427