一,fs
noded.js自带的模板,实现文件的读写
1-1 同步的读取

  1. const fs = require("fs");
  2. /* fs是node.js自带的模块 实现文件的读写 */
  3. var res = fs.readFileSync("./a.json");
  4. console.log(res.toString())

1-2 异步读取

  1. fs.readFile("./a.json",(err,res)=>{
  2. if(err){
  3. throw new Error("读取错误")
  4. }else{
  5. console.log(res.toString())
  6. }
  7. })

二、process进程

  1. process.cwd() //获取当前文件所在的路径
  2. process.env //获取当前应用所在的环境
  3. process.execPath //可以获取执行文件所在的执行环境

1-1 basename

  1. c:/data/logo.jpg
  2. # logo.jpg
  3. path.basename("c:/data/logo.jpg")

1-2 join-resolve

都是解决路径问题

  1. const path = require("path");
  2. var s = path.join(__dirname,"static");
  3. console.log(s)
  4. var t = path.resolve(__dirname,"static");
  5. console.log(t)

1-3 extname 获取文件拓展名

  1. console.log(path.extname(process.execPath))

四、读取静态资源

  1. yarn add koa-static
  1. const koa = require("koa");
  2. const app = new koa();
  3. const path = require("path");
  4. const static = require("koa-static");
  5. app.use(static(path.join(__dirname,"static")))
  6. app.listen(8000);