node.js 入门 (1)

    xiaoxiao2025-02-18  30

    node.js 入门 (1)

    一、node.js 是什么?二、node.js 有那些特点?三、node.js 安装种配置安装方式 1:安装单个 node 版本(不推荐)安装方式 2:安装多个 node 版本(推荐) 四、Node.js REPL(交互式解释器)五、Demodemo1:hello world(helloWorld.js)demo2:文件写入(wfs.js)demo3:单线程node执行顺序(singleton。js)demo4 读文件(rfs.js)demo5 http服务(http.js) 六、干货

    一、node.js 是什么?

    Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型,使其轻量又高效。

    二、node.js 有那些特点?

    事件驱动:当事件被触发时,执行传递过去的回调函数非阻塞式 I/O 的模型:当执行I/O操作时,不会阻塞线程单线程

    三、node.js 安装种配置

    安装方式 1:安装单个 node 版本(不推荐)

    下载地址 https://nodejs.org官网术语解释 LTS 版本:Long-term Support版本,长期支持版,即稳定版。 Current Latest Features版本,当前最新的特性,即最新版本

    安装方式 2:安装多个 node 版本(推荐)

    安装nvm(Node Version Manager) 下载地址:https://github.com/coreybutler/nvm-windows/releases 选择nvm-setup.zip,下载后直接安装。验证安装是否成功:nvm -version 安装node :nvm install 10.15.3 (预计下载5分钟) 使用node:nvm use 10.15.3 常用命令 nvm version 查看版本 nvm install latest nvm install 版本号 nvm uninstall 版本号 nvm list nvm use 版本号

    四、Node.js REPL(交互式解释器)

    Node.js REPL(Read Eval Print Loop:交互式解释器) 表示一个电脑的环境,类似 Window 系统的终端或 Unix/Linux shell,我们可以在终端中输入命令,并接收系统的响应。

    Node 自带了交互式解释器,可以执行以下任务: 读取 - 读取用户输入,解析输入了Javascript 数据结构并存储在内存中。 执行 - 执行输入的数据结构 打印 - 输出结果 循环 - 循环操作以上步骤直到用户两次按下 ctrl-c 按钮退出。

    ye:Node REPL 主要用于调试 Javascript 代码。

    简单的使用 λ node > 5+8 13 > x=5 5 > y=3 3 > x+y 8 > console.log("ye90") ye90 undefined >

    五、Demo

    demo1:hello world(helloWorld.js)

    var m=3; var y=5; function add(x,y){ return x+y; } console.log('计算的结果:',add(m,y)); console.log('hello world!');

    运行结果:

    demo2:文件写入(wfs.js)

    //1.加载文件操作模块,fs模块 var fs = require('fs'); //fs.writeFile(file, data[, options], callback) var msg= 'Hello Node.js'; fs.writeFile('message.txt', msg,'utf8', (err) => { if (err){ throw err; } //ye: err===null,表示写入文件成功 console.log('The file has been saved!'); });

    ye: 1、第三个入参utf8可不写

    demo3:单线程node执行顺序(singleton。js)

    console.log('ye1'); setTimeout(function(){ console.log('ye5'); },3000); console.log('ye2'); setTimeout(function(){ console.log('ye4'); },0); console.log('ye3');

    ye: 1、在线动画演示 http://latentflip.com/loupe

    demo4 读文件(rfs.js)

    var fs= require('fs'); //加载 path 模块 var path = require('path'); //使用join拼接,主要解决兼容,如苹果电脑不支持'\\'这种写法 var filename = path.join(__dirname,'./message.txt'); //fs.readFile(path[, options], callback) fs.readFile(filename,function(err,data){ if (err) { throw err; } // data是一个buffer对象,需要转成字符串 console.log(data.toString('utf8')); // 默认是utf8,所以可不写 console.log(data.toString()); });

    demo5 http服务(http.js)

    var http = require('http') //创建 var server = http.createServer() //监听 server.on('request',function(req,res){ res.write('ye') res.end() }) //启动 server.listen(8080,function(){ console.log('已启动8080端口!') })

    六、干货

    1、异步操作无法通过try-catch来捕获异常,要通过判断error来判断是否出错 2、同步操作可以通过try-catch 来捕获异常 3、不要用 fs.exists(path, callback),exists已弃用,可以使用fs.stat() or fs.access() 替代。也可以直接判断error 4、__dirname:当前被执行的js的目录 5、__filename:当前被执行的js文件名

    最新回复(0)