nodeJS-学习

160 阅读3分钟

入门

JS运行环境

V8引擎 + 内置API

学习路径

nodeJS内置API模块(fs path http)+第三方API模块(express mysql等)

基础命令,常用快捷键

// 版本号
node -v // v12.18.4

// 进入地址
cd
// C盘去到D盘
D:

// 运行
node 1.js

// tab自动补全路径、esc清除当前行、cls/clear清屏

fs模块

fs.readFile

 const fs = require('fs')
 
 // fs.readFile(path, [options], callback)
 // 3个参数,必选:路径,可选:编码格式(如utf8),必选:回调
 
 // 成功情况
 // fs.readFile('./file/1.txt', 'utf8', (err, suc) => {
 //     console.log(err); // null
 //     console.log(suc); // 123(1.txt中的内容)
 // })

 // 失败情况
 // fs.readFile('./file/12.txt', 'utf8', (err, suc) => {
 //     console.log(err); // err
 //     console.log(err.message); // no such file or directory, open 'D:\TS\node\file\12.txt'
 //     console.log(suc); // undefined
 // })

 // 如何判断成功、失败?
 // 根据err的值是否为null

fs.writeFile

const fs = require('fs')

// fs.writeFile(file,data,[options],callback)
// 4个参数
// 必选:文件路径的字符串
// 必选:内容
// 可选:格式(默认utf8)
// 必选:回调


// 成功情况
// fs.writeFile('./file/12.txt', '456', 'utf8', (err) => {
//     console.log(err); // null
// })

// 失败情况
// fs.writeFile('f:/file/12.txt', '456', 'utf8', (err) => {
//     console.log(err); // no such file or directory, open 'f:\file\12.txt'] 
// })

// 需要注意的是:同名文件会覆盖内容,不同名会创建一个新的文件

// 判断写入成功
// 根据err是否为null

实例

把成绩.txtA=90B=100C=77D=101E=23
改写为成绩fin.txtA:90B:100C:77D:101E:23

思路:
// 导入fs
// 读取 成绩.txt
// 判断读取是否成功
// 成功,处理数据
// 写入
const fs = require('fs');
fs.readFile('./file/成绩.txt', 'utf8', (err, suc) => {
    if (err) {
        return console.log('读取失败', err.message);
    }
    const res = suc.replace(/=/g, ':').replace(/ /g, '\n')
    fs.writeFile('./file/成绩-fin.txt', res, 'utf8', (err) => {
        if (err) {
            return console.log('写入失败', err.message);
        }
    })
})

__dirname

相对路径-会导致问题:读取失败 ENOENT: no such file or directory
原因:node执行在当前文件夹下


解决思路:绝对路径,但是移植性差
fs.readFile('D:/TS/node/file/成绩.txt', 'utf8', (err, data) => {
    if (err) {
        return console.log('读取失败', err.message);
    }
    console.log('读取成功', data);
})

__dirname:当前文件所处的目录
console.log(__dirname); // D:\TS\node
fs.readFile(__dirname + '/file/成绩.txt', 'utf8', (err, data) => {
    if (err) {
        return console.log('读取失败', err.message);
    }
    console.log('读取成功', data);
})

path模块

用来处理路径的模块

path.join()

拼接路径字符串

const path = require('path');

// 不建议使用+拼接,∵+不能识别 ../
const path1 = __dirname + '../'
const path2 = path.join(__dirname, '../')
console.log(path1); // D:\TS\node../
console.log(path2); // D:\TS\


// 注:../ 是前往上一层
const path1 = path.join('/a', '/b/c', '../', './d', 'e')
console.log(path1); // \a\b\d\e

const path2 = path.join(__dirname + '/file/成绩.txt')
console.log(path2); // D:\TS\node\file\成绩.txt

path.basename()

获取路径中的文件名

path.basename(path, [ext])
// 两个参数
// 必选:路径
// 可选:文件扩展名

const p1 = 'a/b/c/index.js';
const name1 = path.basename(p1)
console.log(name1); // index.js

const name2 = path.basename(p1, '.js')
console.log(name2); // index

path.extname()

获取路径中的扩展名

const p1 = 'a/b/c/index.js';
const su1 = path.extname(p1)
console.log(su1); // .js

http模块

客户端,服务器

http模块:用来创建web服务器的模块,通过http.createServer()方法

IP地址

ip地址:每台计算机的唯一地址,点分十进制a.b.c.d

ping www.baidu.com // [百度一下,你就知道](http://14.215.177.39/)

127.0.0.1 / localhost // 自己的电脑 

域名、域名服务器

baidu // 域名

14.215.177.39 //IP地址

// DNS服务器(域名服务器)将IP地址转换为域名

端口号

// 每个web服务都对应一个唯一的端口号
// 80端口可以被省略

创建最基本的web服务器

// 1.导入http模块
const http = require('http')

// 2.创建web服务器实例
const server = http.createServer()

// 3.为服务器绑定request事件,监听客户端请求
// req是请求对象,包含了与客户端相关的数据和属性
// req.url是客户端请求的url地址
// req.method是客户端请求的method类型,如get/post
server.on('request', function(req, res) {
    console.log('尝试连接');
    const url = req.url
    const method = req.method
    const str = `url:${url},method:${method}`
    console.log(str); // url:/,method:GET
})

// 4.listen启动服务器
server.listen(8080, function() {
    console.log('服务启动:http://127.0.0.1:8080');
})