24. path
// path.js
const path = require('path');
// window - c:\users\hyungju-lee\
// window 에서 역슬레쉬가 2개씩 있는 경우도 있음
// c:\\users\\hyungju-lee\\
// mac, linux - /user/hyungju-lee/
// 위와 같이 운영체제마다 다른 경로처리를 알아서 해준다.
console.log(path.join(__dirname, 'var.js')); // /Users/xxxx/total/server/node/202305ing_node_study/section_3/3_7_os_path/var.js
// mac + linux 합쳐서 posix 라고 부른다.
// 즉 path 모듈은 window / posix 각각 경우에 따라서 경로를 알아서 설정해준다.
console.log(path.join(__dirname, '..', 'var.js')); // /Users/xxxx/total/server/node/202305ing_node_study/section_3/var.js
console.log(path.resolve(__dirname, '..', 'var.js')); // /Users/xxxx/total/server/node/202305ing_node_study/section_3/var.js
console.log(path.resolve(__dirname, '..', './var.js')); // /Users/xxxx/total/server/node/202305ing_node_study/section_3/var.js
console.log(path.resolve(__dirname, '..', '/var.js')); // /var.js
console.log(path.join(__dirname, '..', '/var.js')); // /Users/xxxx/total/server/node/202305ing_node_study/section_3/var.js
// join 메서드는 절대 경로를 무시한다.
// resolve 메서드는 절대 경로를 무시하지 않는다.
console.log(path.sep); // / // <- 경로 구분자 (window 는 \, posix 는 /)
console.log(path.delimiter); // : // <- 환경변수 구분자 (window 는 ; 세미콜론, posix 는 :)
console.log(path.dirname(__filename)); // /Users/xxxx/total/server/node/202305ing_node_study/section_3/3_7_os_path
console.log(path.extname(__filename)); // .js
console.log(path.basename(__filename)); // path.js
console.log(path.basename(__filename, path.extname(__filename))); // path
console.log(path.parse(__filename));
// {
// root: '/',
// dir: '/Users/xxxx/total/server/node/202305ing_node_study/section_3/3_7_os_path',
// base: 'path.js',
// ext: '.js',
// name: 'path'
// }
console.log(path.format({
root: '/',
dir: '/Users/xxxx/total/server/node/202305ing_node_study/section_3/3_7_os_path',
base: 'path.js',
ext: '.js',
name: 'path'
}
))
// /Users/xxxx/total/server/node/202305ing_node_study/section_3/3_7_os_path/path.js
console.log(path.normalize('/user\\hyungju-lee\\nodejs///study')); // /user\hyungju-lee\nodejs/study
console.log(path.isAbsolute('/')); // true
console.log(path.isAbsolute('/user')); // true
console.log(path.isAbsolute('./user')); // false
console.log(path.relative('/users/hyungju-lee/nodejs/study', '/users')); // ../../..
console.log(path.join(__dirname, '..', '..', '/users', '.', '/hyungju-lee')); // /Users/xxxx/total/server/node/202305ing_node_study/users/hyungju-lee