fs 讀取文件模塊
let oldtext = ''
// 讀取
fs.readFile(__dirname + '/input.txt', 'utf-8', (err, text) => {
if(err) { return console.log(err) }
oldtext = text
console.log(text)
})
// 寫
fs.writeFile(__dirname + '/input.txt', oldtext + '\r\nconsole.log("hello world!")', (err, text) => {
if(err) { return console.log(err) }
console.log(text)
})
__dirname :代表當前js文件所在目錄的路徑(絕對路徑)(也就是當前js的上一級目錄), 如果直接用相對路徑 './input.txt', 如果在不同級文件運行命令則會報錯,所以要用絕對路徑或這種拼接。
__filename: 代表當前js文件的路徑(絕對路徑)
path 路徑模塊
path.join() 拼接成路徑 './' '/' '../'自動整合
path.join(__dirname, 'input.txt')
path.basename
獲取路徑最后一部分 文件名+后綴
path.basename('E:/web/node/uploadServer/input.txt') // input.txt
path.basename('E:/web/node/uploadServer/input.txt', '.txt') // input
path.extname
獲取后綴
path.extname('E:/web/node/uploadServer/input.txt') // .txt
小作業
input.txt 小紅=99 六=55 阿斯頓=12 問我=33 后端=12 轉換成

const fs = require('fs')
const path = require('path')
let handleText = ''
fs.readFile(path.join(__dirname, 'input.txt'), 'utf-8', (err, text) => {
if(err) { return console.log(err) }
handleText = text.split(' ').map(item => item.replace(/=/, ':')).join('\r\n')
fs.writeFile(path.join(__dirname, 'input.txt'), handleText, (err, text) => {
if(err) { return console.log(err) }
console.log(text)
})
})
小作業2
將html文件種的 html js css 分離 出來
html chatRoom.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>聊天室</title>
<style>
li{
list-style: none;
}
</style>
</head>
<body>
<h3>聊天室</h3>
<p>當前在線人數:<span class="total">0</span></p>
<ul></ul>
<input type="text" placeholder="請輸入發送信息">
<button>發送</button>
<script>
const btn = document.querySelector('button')
const input = document.querySelector('input')
const ul = document.querySelector('ul')
const totalDom = document.querySelector('.total')
const ws = new WebSocket('ws://localhost:8000')
let userName = ''
let total = 0
btn.addEventListener('click', sendMsg, false)
ws.addEventListener('open', () => {
console.log('前端:連接成功')
userName = location.href.split('?')[1].split('=')[1]
ws.send(JSON.stringify({ text: userName + '進入了聊天室'}))
})
ws.addEventListener('message', getMsg, false)
ws.addEventListener('error', () => {console.warn('前端:連接失敗')})
ws.addEventListener('close', () => {console.warn('前端:斷開連接')})
function sendMsg() {
userName = location.href.split('?')[1].split('=')[1]
ws.send(JSON.stringify({ name: userName, time: new Date(), text: input.value}))
}
function getMsg(e) {
console.log('前端:獲取聊天消息成功:' + e.data)
const msg = JSON.parse(e.data)
const li = document.createElement('li')
totalDom.textContent = msg.total
li.innerHTML = `
<span><strong>${msg.name || ''}</strong><span> <span>${msg.time || ''}</span>
<br/>
<span>${msg.text}</span>
`
ul.appendChild(li)
}
</script>
</body>
</html>
node index.js
const fs = require('fs')
const path = require('path')
// 讀取html文件
fs.readFile(path.join(__dirname, 'public/chatRoom.html'), 'utf-8', (err, text) => {
if (err) { return console.log(err) }
const regGetScript = /<script>([\s\S]*)<\/script>/
const regGetStyle = /<style>([\s\S]*)<\/style>/
let js = regGetScript.exec(text)[1].replace(/(\s){2,10000}/g, ';') // 匹配兩個空格以上的轉為;
const exString = /`(.*)`/g.exec(js)[0].replace(/;/g, '') // 將模板字符串里的;去除
const style = regGetStyle.exec(text)[1].replace(/\s/g, '')
const html = text.replace(/(\s){2,}|(<script>[\s\S]*<\/script>)|(<style>[\s\S]*<\/style>)/g, '') // 去掉空格
js = js.replace(/`.*`/g, exString)
// 創建 dist 文件
fs.mkdir(path.join(__dirname, './dist'), {
recursive: true
}, (err) => {
if (err) { return console.log(err) }
fs.writeFile(path.join(__dirname, 'dist/index.js'), js, (err, text) => {
if (err) { console.log(err) }
console.log(text)
})
fs.writeFile(path.join(__dirname, 'dist/index.css'), style, (err, text) => {
if (err) { return console.log(err) }
console.log(text)
})
fs.writeFile(path.join(__dirname, 'dist/index.html'), html, (err, text) => {
if (err) { return console.log(err) }
console.log(text)
})
})
})
最后打包結果

人生很漫長,或許我只是你人生中微不足道的一小段,只是你人生中的驚鴻一瞥。
浙公網安備 33010602011771號