一,fs
noded.js自带的模板,实现文件的读写
1-1 同步的读取
const fs = require("fs");
/* fs是node.js自带的模块 实现文件的读写 */
var res = fs.readFileSync("./a.json");
console.log(res.toString())
1-2 异步读取
fs.readFile("./a.json",(err,res)=>{
if(err){
throw new Error("读取错误")
}else{
console.log(res.toString())
}
})
二、process进程
process.cwd() //获取当前文件所在的路径
process.env //获取当前应用所在的环境
process.execPath //可以获取执行文件所在的执行环境
1-1 basename
c:/data/logo.jpg
# logo.jpg
path.basename("c:/data/logo.jpg")
1-2 join-resolve
都是解决路径问题
const path = require("path");
var s = path.join(__dirname,"static");
console.log(s)
var t = path.resolve(__dirname,"static");
console.log(t)
1-3 extname 获取文件拓展名
console.log(path.extname(process.execPath))
四、读取静态资源
yarn add koa-static
const koa = require("koa");
const app = new koa();
const path = require("path");
const static = require("koa-static");
app.use(static(path.join(__dirname,"static")))
app.listen(8000);