Search

path

path

Node.js์—์„œ path ๋ชจ๋“ˆ์€ ํŒŒ์ผ ๋ฐ ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ชจ๋“ˆ์€ ๊ฒฝ๋กœ๋ฅผ ์กฐ์ž‘ํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ์œ ์šฉํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ช‡ ๊ฐ€์ง€ ์ฃผ์š” ๊ธฐ๋Šฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
โ€ข
path.join(): ์—ฌ๋Ÿฌ ๊ฒฝ๋กœ ์„ธ๊ทธ๋จผํŠธ๋ฅผ ํ•˜๋‚˜์˜ ๊ฒฝ๋กœ๋กœ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค.
โ€ข
path.resolve(): ์ฃผ์–ด์ง„ ๊ฒฝ๋กœ๋ฅผ ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋กœ ํ•ด์„ํ•ฉ๋‹ˆ๋‹ค.
โ€ข
path.basename(): ํŒŒ์ผ ๊ฒฝ๋กœ์—์„œ ํŒŒ์ผ์˜ ๊ธฐ๋ณธ ์ด๋ฆ„์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
โ€ข
path.dirname(): ํŒŒ์ผ ๊ฒฝ๋กœ์—์„œ ํŒŒ์ผ์ด ์œ„์น˜ํ•œ ๋””๋ ‰ํ† ๋ฆฌ ์ด๋ฆ„์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
โ€ข
path.extname(): ํŒŒ์ผ ๊ฒฝ๋กœ์—์„œ ํŒŒ์ผ์˜ ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
โ€ข
path.parse(): ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๋ถ„์„ํ•˜์—ฌ ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
path ๋ชจ๋“ˆ์€ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์กฐ์ž‘ํ•ด์•ผ ํ•  ๋•Œ ์œ ์šฉํ•˜๋ฉฐ, ํŒŒ์ผ ๋ฐ ๋””๋ ‰ํ† ๋ฆฌ ๊ด€๋ฆฌ์— ํ•„์ˆ˜์ ์ธ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

์ •์˜

path ๋ชจ๋“ˆ์€ ํŒŒ์ผ ๋ฐ ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ์™€ ๊ด€๋ จ๋œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” Node.js์˜ ๋‚ด์žฅ ๋ชจ๋“ˆ์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์กฐ์ž‘ํ•˜๋ฉฐ, ๊ฒฝ๋กœ์˜ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ถ”์ถœํ•˜๋Š” ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

์ฃผ์š” ์†์„ฑ ๋ฐ ๋ฉ”์†Œ๋“œ

๋ฉ”์„œ๋“œ/์†์„ฑ
์„ค๋ช…
sep
ํ”Œ๋žซํผ๋ณ„ ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ (์Šฌ๋ž˜์‹œ ๋˜๋Š” ์—ญ์Šฌ๋ž˜์‹œ)
delimiter
ํ”Œ๋žซํผ๋ณ„ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ PATH์˜ ๊ฒฝ๋กœ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ
dirname
๊ฒฝ๋กœ์˜ ๋””๋ ‰ํ† ๋ฆฌ ๋ถ€๋ถ„์„ ๋ฐ˜ํ™˜
extname
ํŒŒ์ผ์˜ ํ™•์žฅ์ž๋ฅผ ๋ฐ˜ํ™˜
basename
ํŒŒ์ผ์˜ ๊ธฐ๋ณธ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜
parse
ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ํŒŒ์‹ฑํ•˜์—ฌ ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜
format
ํŒŒ์ผ ๊ฒฝ๋กœ ๊ฐ์ฒด๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜
normalize
๊ฒฝ๋กœ๋ฅผ ์ •๊ทœํ™”ํ•˜์—ฌ ์ •์ƒ์ ์ธ ํ˜•ํƒœ๋กœ ๋งŒ๋“ฆ
isAbsolute
๊ฒฝ๋กœ๊ฐ€ ์ ˆ๋Œ€ ๊ฒฝ๋กœ์ธ์ง€ ํ™•์ธ
relative
๋‘ ๊ฒฝ๋กœ ๊ฐ„์˜ ์ƒ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ๋ฐ˜ํ™˜
join
์—ฌ๋Ÿฌ ๊ฒฝ๋กœ๋ฅผ ๊ฒฐํ•ฉํ•˜์—ฌ ์ „์ฒด ๊ฒฝ๋กœ ์ƒ์„ฑ
resolve
์ƒ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋กœ ๋ณ€ํ™˜

๋ฌธ๋ฒ•

path ๋ชจ๋“ˆ์€ Node.js์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณต๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„์˜ ์„ค์น˜ ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด require๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
const path = require('path');
JavaScript
๋ณต์‚ฌ
์ฃผ์˜: ES6์˜ import ๊ตฌ๋ฌธ์€ ํ˜„์žฌ์˜ Node.js์—์„œ๋Š” ๋‚ด์žฅ ๋ชจ๋“ˆ์— ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ os ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด require๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค:

์˜ˆ์‹œ์ฝ”๋“œ

const path = require('path'); const filePath = '/path/to/some/file.txt'; // ๊ฒฝ๋กœ์˜ ๊ธฐ๋ณธ ์ •๋ณด console.log('Directory:', path.dirname(filePath)); console.log('Filename:', path.basename(filePath)); console.log('Extension:', path.extname(filePath)); // ๊ฒฝ๋กœ ๊ฒฐํ•ฉ const absolutePath = path.join('/path', 'to', 'file.txt'); console.log('Absolute Path:', absolutePath); // ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋กœ ๋ณ€ํ™˜ const absolutePathFromRelative = path.resolve('folder', 'file.txt'); console.log('Absolute Path from Relative:', absolutePathFromRelative);
JavaScript
๋ณต์‚ฌ

์‚ฌ์šฉ ๋ชฉ์ 

path ๋ชจ๋“ˆ์€ ๋‹ค์–‘ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ ์ฃผ์š” ๋ชฉ์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:
โ€ข
๊ฒฝ๋กœ ์กฐ์ž‘: ๊ฒฝ๋กœ์˜ ๊ธฐ๋ณธ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜๊ฑฐ๋‚˜ ๊ฒฝ๋กœ๋ฅผ ๊ฒฐํ•ฉํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
โ€ข
๊ฒฝ๋กœ ํ•ด์„: ์ƒ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋กœ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜ ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ์ƒ๋Œ€ ๊ฒฝ๋กœ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
โ€ข
ํ”Œ๋žซํผ ๋ณ„ ๊ฒฝ๋กœ: ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ”Œ๋žซํผ์— ๋”ฐ๋ผ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
โ€ข
๊ฒฝ๋กœ ์ •๊ทœํ™”: ๊ฒฝ๋กœ์—์„œ ์ƒ๋Œ€ ๊ฒฝ๋กœ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์ •๊ทœํ™”ํ•ฉ๋‹ˆ๋‹ค.
path ๋ชจ๋“ˆ์€ ํŒŒ์ผ ์‹œ์Šคํ…œ๊ณผ ๊ด€๋ จ๋œ ์ž‘์—…์„ ํ•  ๋•Œ ํŠนํžˆ ์œ ์šฉํ•˜๋ฉฐ, ๊ฒฝ๋กœ์™€ ๊ด€๋ จ๋œ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ๊ฐ„ํŽธํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์›€์„ ์ค๋‹ˆ๋‹ค.