首页 > 自考资讯 > 自考知识

node.js使用教程,node.js教程详细

头条共创 2024-06-27

示例1. 跟大家说 Hello

因为Node.js 只是JavaScript 程序的执行环境,所以它并不限制您执行JavaScript 代码的任何特定形式。为了说明这一点,本示例使用四种格式向每个人打招呼“Hello”。首先,您需要在代码目录中运行mkdir 01_sayHello 命令创建一个目录来保存这组示例。

1. 终端输出

要将信息打印到终端,必须调用console.log() 方法。步骤如下:

1. 运行code/01_sayHello目录下的touch 01-sayHello.js命令。 2. 运行vim 01-sayHello.js命令打开脚本文件。 3. 在01-sayHello.js脚本文件中输入以下代码。 const name='owlman' console.log('Hello!', name) //输出:保存Hello! 文件后,在code/01_sayHello 目录中运行node 01-sayHello.js 命令,您将看到输出。

e03752d8a74c4107aac5a1438639d62b~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1720092827&x-signature=1cWZlqXtEUB1KO%2FWZEagwCIVNSw%3D

2. 文件读写

要使用Node.js 脚本读取文本文件,必须调用fs.readFile() 方法。以下是具体步骤:

1. 运行code/01_sayHello目录下的touch 01-readFile.js命令。 2. 在code/01_sayHello目录下执行mkdir data命令,创建文件存放目录。 3. 在code/01_sayHello/data目录下运行touch text-data.txt命令,输入以下内容: 你好owlman,欢迎学习Nodejs。然后读下面这首诗。朝十白帝在彩云中,千里进墓,一日回返。岸边的猴子止不住啼叫,船过了万山。 4、在code/01_sayHello目录下执行vim 01-readFile.js命令,打开脚本文件,输入以下代码。 const fs=require('fs') fs.readFile('./data/text-data .txt', function (err, data) { if (err !==null ) { return console.error('错误信息: ' + err.message) } console.log(data.toString()) })5. 保存文件后,在code/01_sayHello目录下运行node 01-readFile.js命令,相关内容将会输出到您可以看到:b1c37aa5c19244289f1a3b7a7eff8fa9~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1720092827&x-signature=fSym9NL6Ya1K0OJAmfuobvqzSp4%3D 要使用Node.js 脚本写入文本文件,您需要调用fs.writeFile() 方法。以下是具体步骤:

1. 在code/01_sayHello目录下运行touch 01-writeFile.js命令。 2、在code/01_sayHello目录下执行vim 01-writeFile.js命令,打开脚本文件,输入以下代码。 const fs=require('fs') const str='你好,Nodejs! ' fs.writeFile('./data/output.txt', str, function(err) { if (err !==null ) { return console .error('错误信息:' + err.message) } console.log('文件写入成功!') }) 3、保存文件后,在code/01_sayHello 目录下写入节点01-writeFile 时运行。 js命令,你会看到相关内容被写入到Output.txt文件中。4779b85281bf49c28bf9c7822c03c0a9~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1720092827&x-signature=w7KDiV0KUrjWc61xmdi9TKySIV4%3D

3. Web 服务

要使用Node.js 脚本创建Web 服务,必须使用http 模块。以下是具体步骤:

在code/01_sayhello目录下运行touch 01-webserver.js命令。运行vim 01-webServer.js命令打开脚本文件,输入以下代码。 const http=require('http') const server=http.createServer() server.on('request', function(req, res) ){ res.end('h1 Hello Nodejs!/h1') })server . Listen(8080, function(){ console.log('http://localhost:8080/并按Ctrl+C 终止服务!') } ) 3. 保存文件。 然后在code/01_sayHello目录下运行node 01-webServer.js命令,使用浏览器访问http://localhost:8080/,你会看到一个带有h1标签的页面:531913b3c1c84b1da5892649e7ec44f1~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1720092827&x-signature=n4tYPtDUZ0MvUL7R%2F0%2Bdb6rJ0Vk%3D

4. TCP 服务

Node.js 我需要构建一个TCP服务使用具体步骤是:

1. 运行code/01_sayHello目录下的touch 01-tcpServer.js命令。 2. 运行vim 01-tcpServer.js命令打开脚本文件,输入以下代码。 const net=require('net') const server=net.createServer(function (socket) { console.log('来自' +socket.remoteAddress 的连接)socket.end('你好,Nodejs!\n') })server .listen(7000, 'localhost', function(){ console.log('TCP 服务器监听localhost 上的7000 端口。Ctrl+C 结束服务!') }) 3.保存文件然后运行节点01-在code/01_sayHello 目录中运行tcpServer.js 命令,并使用telnet localhost 7000 命令测试服务器。结果如下。0f0de6e1ddc44b978dc40b0cc9d04cd0~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1720092827&x-signature=YW2wQVi20T7dZ6nJ48c9CtCGVs4%3D

示例2. 自定义模块及其使用方式

这个示例专门演示Node.js 中的自定义模块语法和ES6 中的一些新语法。首先,您需要通过在代码目录中运行mkdir 02_useModule 命令来为这组示例创建一个目录。

1. 单文件模块

1. 在code/02_useModule目录下运行touch 02-singleFile.js命令创建脚本文件,并输入以下代码。 class singleFile_module {constructor() { this.name='singleFile_module' } Sayhello() { console.log('Hello', this.name) } } module.exports=singleFile_module2,触摸code/02_useModule 目录中的02-test.js运行命令,创建脚本文件,并输入以下代码。 require('./02-singleFile') const testobj=new singleFile_module() testobj.sayhello() 3.保存文件后,在code/02_useModule目录下运行node 02-test.js命令。结果如下。7ef3e62fda7741a3b0861ffe1dff0d81~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1720092827&x-signature=4gofhDnVOwsXdWO2%2FMh2cVin%2F0U%3D

2. 多文件模块

1. 在code/02_useModule目录下执行mkdir 02-multiFile命令,进入该目录,执行npm init -y命令创建模块目录。 2. 在code/02_useModule/02-multiFile 目录下运行touch function.js 命令创建脚本文件,并输入以下代码。函数add(x,y){ 返回x + y} 导出.add=add3.在code/02_useModule/02-multiFile目录下运行touch dataObj.js命令,创建脚本文件,输入以下代码。在code/中运行const name='multFile_module'exports.name=name4。在02_useModule/02-multiFile目录中使用命令创建脚本文件,并输入以下代码。 const func=require('./functions') const str=require('./dataObj') class multiFile_module { constructor() { this. func=func.add this.name=str.name } Sayhello() { console.log() 。 log('Hello', this.name) console.log('x + y=', this.func(10,5)) } 修改code/02_useModule目录下的02-test.js脚本如下: const singleFile_module=require(`./02-singleFile`) const testobj=new singleFile_module() testobj.sayhello() const multiFile_module=require(`./02-multiFile`) const testobj2=new multiFile_module() testobj2.sayhello()保存所有文件后,在code/02_useModule目录下运行node 02-test.js命令,结果为:以下:8b9c8a3eb6bd4b7f953c49fed2770817~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1720092827&x-signature=p0U6HnztAGZseP2HAejDfYYwOnw%3D

版权声明:本文由今日头条转载,如有侵犯您的版权,请联系本站编辑删除。

猜你喜欢