728x90
1. path 모듈
Node.js에서 파일과 디렉토리 경로를 다루기 위한 내장 모듈
파일 시스템 경로를 조작하고, 플랫폼 간 호환성을 보장하는 데 유용한 기능들을 제공합니다.
2.기능
2-1.경로 결합 (path.join)
const fullPath = path.join('/users', 'john', 'docs', 'file.txt');
console.log(fullPath); // '/users/john/docs/file.txt'
2-2.절대 경로 생성 (path.resolve)
const path = require('path');
const absolutePath = path.resolve('users', 'john', 'docs', 'file.txt');
console.log(absolutePath); // '/current/working/directory/users/john/docs/file.txt'
2-3.파일명 추출 (path.basename)
const path = require('path');
const filename = path.basename('/users/john/docs/file.txt');
console.log(filename); // 'file.txt'
const filenameWithoutExt = path.basename('/users/john/docs/file.txt', '.txt');
console.log(filenameWithoutExt); // 'file'
2-4.디렉토리명 추출 (path.dirname)
const path = require('path');
const dirname = path.dirname('/users/john/docs/file.txt');
console.log(dirname); // '/users/john/docs'
2-5.파일 확장자 추출 (path.extname)
const path = require('path');
const ext = path.extname('/users/john/docs/file.txt');
console.log(ext); // '.txt'
2-6.경로 파싱 (path.parse)
const path = require('path');
const parsedPath = path.parse('/users/john/docs/file.txt');
console.log(parsedPath);
/*
{
root: '/',
dir: '/users/john/docs',
base: 'file.txt',
ext: '.txt',
name: 'file'
}
*/
2-7.경로 포맷팅 (path.format)
const path = require('path');
const formattedPath = path.format({
root: '/',
dir: '/users/john/docs',
base: 'file.txt',
ext: '.txt',
name: 'file'
});
console.log(formattedPath); // '/users/john/docs/file.txt'
728x90
'BackEnd > Node.js' 카테고리의 다른 글
[Node.js] sanitize-html 모듈 (0) | 2024.09.04 |
---|---|
[Node.js] fs 모듈 (1) | 2024.09.04 |
[Node.js] querystring 모듈 (0) | 2024.09.04 |