node.js中的path.join方法使用说明
方法说明:
将多个参数组合成一个 path (详细请看例子)
语法:
path.join([path1], [path2], [...])
由于该方法属于path模块,使用前需要引入path模块(var path= require(“path”) )
例子:
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..') // returns '/foo/bar/baz/asdf' path.join('foo', {}, 'bar') // throws exception TypeError: Arguments to path.join must be strings
源码:
// windows version exports.join = function() { function f(p) { if (!util.isString(p)) { throw new TypeError('Arguments to path.join must be strings'); } return p; } var paths = Array.prototype.filter.call(arguments, f); var joined = paths.join('\'); // Make sure that the joined path doesn't start with two slashes, because // normalize() will mistake it for an UNC path then. // // This step is skipped when it is very clear that the user actually // intended to point at an UNC path. This is assumed when the first // non-empty string arguments starts with exactly two slashes followed by // at least one more non-slash character. // // Note that for normalize() to treat a path as an UNC path it needs to // have at least 2 components, so we don't filter for that here. // This means that the user can use join to construct UNC paths from // a server name and a share name; for example: // path.join('//server', 'share') -> '\\server\share') if (!/^[\/]{2}[^\/]/.test(paths[0])) { joined = joined.replace(/^[\/]{2,}/, '\'); } return exports.normalize(joined); };
node.js中的path.isAbsolute方法使用说明
方法说明:检测path是否为绝对路径。一个绝对路径会解析到相同的位置,无论是不是在工作目录。语法:path.isAbsolute(path)由于该方法属于path模块,使用
Nodejs极简入门教程(一):模块机制
JavaScript规范(ECMAScript)没有定义一套完善的能适用于大多数程序的标准库。CommonJS提供了一套JavaScript标准库规范。Node实现了CommonJS规范。模块基础在Nod
Nodejs极简入门教程(二):定时器
setTimeout和clearTimeoutvarobj=setTimeout(cb,ms);setTimeout用于设置一个回调函数cb,其在最少ms毫秒后被执行(并非在ms毫秒后马上执行)。setTimeout返回值可以作为c