1、process

  1. process.cwd()获取当前文件所在路径
  2. console.log(process.cwd());//C:\Users\chengdong\Documents\vs\Node\fs模块
  3. process.env 获取当前应用所在的环境
  4. console.log(process.env);
  5. console.log(process.execPath);
  6. process.execPath 获取执行文件所在的执行环境

2、path

path模块是node.js自带的模块,主要是为了解决服务器端路径问题而诞生的

  1. const path = require("path")
  2. console.log(__dirname);获取当前文件所在路径

2-1 basename

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

2-2 join-resolve

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

2-3 extname 获取文件扩展名

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

3、static

读取静态资源

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