1 Node.js
1.1 什么是Node.js
定义:Node.js是一个跨平台JavaScript运行环境,开发者可以搭建服务端的JavaScript应用程序
1.2 Node.js为什么能执行JS
2 Node.js的使用
2.1 Node.js安装与使用
2.1.1 Node.js的安装
cmd查看版本号:
现在推荐16.19.1(兼容此课程剩下项目的版本)
2.1.2 Node.js的使用
注意:需要确认终端路径和要使用的文件路径统一
2 fs模块——读写文件
/**
* 目标:读取 test.txt 文件内容
* 注意:代码中,使用绝对路径
* 原因:Node.js 执行时会以终端所在文件夹作为相对路径,去拼接代码中路径使用(导致找不到目标文件)
* 解决:使用 path.join() 和 __dirname 来填写要查找的目标文件绝对地址
*/
// 1. 引入fs模块
const fs = require('fs')
// console.log(fs)
// 1.写入方法(如果不存在,则创建)
fs.writeFile('./try.txt', '你好,我是candelet', err => {
if (err !== null) {
console.log('写入失败')
}
})
fs.readFile('./try.txt', (err, data) => {
if (err) { console.log(err) }
else {
console.log(data.toString())
}
})
结果:
3 Path模块——路径处理
(Node.js必须使用绝对路径,所以需要使用path.join(__dirname,'查询路径'))来查询
/**
* 目标:读取 test.txt 文件内容
* 注意:代码中,使用绝对路径
* 原因:Node.js 执行时会以终端所在文件夹作为相对路径,去拼接代码中路径使用(导致找不到目标文件)
* 解决:使用 path.join() 和 __dirname 来填写要查找的目标文件绝对地址
*/
// 1. 引入fs模块
const fs = require('fs')
// 在nodejs中,最好使用绝对路径
fs.readFile('E:/DeskTop/13.案例_学习反馈/test.txt', (err, data) => {
if (err) {
return console.log(err)
}
// 将Buffer转换为字符串并打印
console.log('文件内容:', data.toString())
})
console.log(__dirname)//当前文件所在的目录
结果:
使用path.join(__dirname(当前文件名),‘路径’)
/**
* 目标:读取 test.txt 文件内容
* 注意:代码中,使用绝对路径
* 原因:Node.js 执行时会以终端所在文件夹作为相对路径,去拼接代码中路径使用(导致找不到目标文件)
* 解决:使用 path.join() 和 __dirname 来填写要查找的目标文件绝对地址
*/
// 1. 引入fs模块
const { log } = require('console')
const fs = require('fs')
const path = require('path')
// console.log(__dirname)//输出当前文件所在目录
// path.join(__dirname, '../test.txt')
// path.join 拼接路径字符串,会以当前的系统环境进行分隔符的设置
// console.log(path.join('E', 'DeskTop', '13.案例_学习反馈', 'candelet.js'))
console, log(path.join(__dirname, 'test.txt'))
结果:
4 案例
4.1 案例-压缩前端html
4.2 压缩前端JS
/**
* 目标一:压缩 html 里代码
* 需求:把 public/index.html 里的,回车/换行符去掉,写入到 dist/index.html 中
* 1.1 读取 public/index.html 内容
* 1.2 使用正则替换内容字符串里的,回车符\r 换行符\n
* 1.3 确认后,写入到 dist/index.html 内
*/
const fs = require('fs')
const path = require('path')
// 读取文件
fs.readFile(path.join(__dirname, 'public', 'index.html'), (err, data) => {
const htmlStr = data.toString()
const resultStr = htmlStr.replace(/[\r\n]/g, '')
// 读取index.html内容
fs.readFile(path.join(__dirname, 'public', 'index.js'), (err, data) => {
if (err) console.log(err)
else {
const jsStr = data.toString()
const resultJs = jsStr.replace(/[\r\n]/g, '').replace(/console.log\('.+?'\);/, '')
// console.log(resultJs)
const jsscript = `<script>${resultJs}</script>`
// 写入新的html中
fs.writeFile(path.join(__dirname, 'dist', 'index.html'), resultStr + jsscript, (err) => {
if (err) console.log(err)
else {
console.log('压缩成功')
}
})
}
})
})
结果:
dist文件夹下面写入了一个
index.html文件(已压缩)
5 URL端口号
6 http模块-创建web服务
6.1 基本代码
访问结果:localhost:3000
6.2 返回中文字符
const http = require('http');//http fs path 是nodejs的内置模块
const server = http.createServer()
//2.监听request请求事件,这个事件会监听浏览器的请求地址,并做出相应
server.on('request', (req, res) => {
// 通过设置相应头,解决中文乱码问题
// 给响应设置响应头,键‘Content-Type'内容类型
// 值是'text/html','charset=utf-8' 普通文本,万国码解析方式
res.setHeader('Content-Type', 'text/html; charset=utf-8')
res.end('hello nodejs')
})
//3.启动服务器,并告诉服务器需要的端口号是什么
server.listen(3000, () => {
console.log('3000端口启动成功')
})
结果;
7 案例
7.1 获取省份列表-接口开发
const http = require('http')
const server = http.createServer()
const path = require('path')
const fs = require('fs')
server.on('request', (req, res) => {
// 2. 使用req.url获取请求的资源路径,读取json文件数据并且返回
// console.log('req.url', req.url)
if (req.url === '/api/province') {
// console.log('api/province')
//当客户端访问/api/province时,返回一个json数据
fs.readFile(path.join(__dirname, 'public/province.json'), (err, data) => {
if (err) {
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
return res.end('404')
}
res.end(data)
})
} else {
res.setHeader('Content-Type', 'text/plain; charset=utf-8')
res.end('success')
}
})
server.listen(3000, () => {
console.log('server running at http://127.0.0.1:3000')
})
结果: