|
3 | 3 | This module contains utilities for dealing with file paths. Use |
4 | 4 | `require('path')` to use it. It provides the following methods: |
5 | 5 |
|
| 6 | +### path.normalize(p) |
| 7 | + |
| 8 | +Normalize a string path, taking care of `'..'` and `'.'` parts. |
| 9 | + |
| 10 | +When multiple slashes are found, they're replaces by a single one; |
| 11 | +when the path contains a trailing slash, it is preserved. |
| 12 | +On windows backslashes are used. |
| 13 | + |
| 14 | +Example: |
| 15 | + |
| 16 | + path.normalize('/foo/bar//baz/asdf/quux/..') |
| 17 | + // returns |
| 18 | + '/foo/bar/baz/asdf' |
| 19 | + |
6 | 20 | ### path.join([path1], [path2], [...]) |
7 | 21 |
|
8 | | -Join all arguments together and resolve the resulting path. |
| 22 | +Join all arguments together and normalize the resulting path. |
9 | 23 |
|
10 | 24 | Example: |
11 | 25 |
|
12 | 26 | node> require('path').join( |
13 | 27 | ... '/foo', 'bar', 'baz/asdf', 'quux', '..') |
14 | 28 | '/foo/bar/baz/asdf' |
15 | 29 |
|
16 | | -### path.normalizeArray(arr) |
| 30 | +### path.resolve([from ...], to) |
17 | 31 |
|
18 | | -Normalize an array of path parts, taking care of `'..'` and `'.'` parts. |
| 32 | +Resolves `to` to an absolute path name and normalizes it. |
19 | 33 |
|
20 | | -Example: |
| 34 | +One ore more `from` arguments may be provided to specify the the starting |
| 35 | +point from where the path will be resolved. `resolve` will prepend `from` |
| 36 | +arguments from right to left until an absolute path is found. If no `from` |
| 37 | +arguments are specified, or after prepending them still no absolute path is |
| 38 | +found, the current working directory will be prepended eventually. |
21 | 39 |
|
22 | | - path.normalizeArray(['', |
23 | | - 'foo', 'bar', 'baz', 'asdf', 'quux', '..']) |
24 | | - // returns |
25 | | - [ '', 'foo', 'bar', 'baz', 'asdf' ] |
| 40 | +Trailing slashes are removed unless the path gets resolved to the root |
| 41 | +directory. |
26 | 42 |
|
27 | | -### path.normalize(p) |
| 43 | +Examples: |
28 | 44 |
|
29 | | -Normalize a string path, taking care of `'..'` and `'.'` parts. |
| 45 | + path.resolve('index.html') |
| 46 | + // returns |
| 47 | + '/home/tank/index.html' |
30 | 48 |
|
31 | | -Example: |
| 49 | + path.resolve('/foo/bar', './baz') |
| 50 | + // returns |
| 51 | + '/foo/baz/baz' |
32 | 52 |
|
33 | | - path.normalize('/foo/bar/baz/asdf/quux/..') |
| 53 | + path.resolve('/foo/bar', '/tmp/file/') |
34 | 54 | // returns |
35 | | - '/foo/bar/baz/asdf' |
| 55 | + '/tmp/file' |
| 56 | + |
| 57 | + path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif') |
| 58 | + // returns |
| 59 | + '/home/tank/wwwroot/static_files/gif/image.gif' |
36 | 60 |
|
37 | 61 | ### path.dirname(p) |
38 | 62 |
|
|
0 commit comments