8

我有两个动态的 URL 片段,我试图将它们连接在一起以形成一个完整的 URL。由于我不知道我将要加入的确切字符串,我想使用路径加入库来避免字符串加入错误,例如"http://www.mysite.com/friends//12334.html",它有一个额外的斜杠等。

我正在使用 Node.js 在 Windows 7 家庭计算机上工作。

我尝试使用path库的path.join(...),但是因为我在 Windows 上,它把所有的正斜杠都向后转,这对于 URL 显然是不正确的。例子:

var path = require('path'),
    joined = path.join('http://www.mysite.com/', '/friends/family');

console.log(joined);
// Prints:
// http:\www.miserable.com\friends\family

我可以使用什么函数或库在 Windows 上将 URL 片段连接在一起?或者,我怎样才能path.join强制使用 UNIX 样式的分隔符而不是 Windows 的分隔符?

4

3 回答 3

5

URL 不是文件系统路径,因此其中的任何内容都不path适用于您的要求。url.resolve()如果它满足您的需求,我建议使用,否则url.format()。请注意,您不能简单地替换path.join()代码中的任何一个,因为它们需要不同的参数。仔细阅读文档。

于 2012-10-04T08:13:22.720 回答
3

...我怎样才能让 path.join 强制使用 UNIX 样式的分隔符而不是 Windows 的分隔符?

虽然(如@ebohlman 所述)URL 不是文件系统路径,path.posix.join()但可用于创建URL.pathname组件。

const path = require('path');
const url = require('url');

const origin = 'http://www.example.com/';
const pathname = path.posix.join(path.posix.sep, 'friends', 'family');

// All at once...
const myURL = new URL(pathname, origin);
console.log(myURL.href);
// => 'http://www.example.com/friends/family'

// In steps...
const myURL2 = new URL(origin);
console.log(myURL2.href);
// => 'http://www.example.com/'
myURL2.pathname = pathname;
console.log(myURL2.href);
// => 'http://www.example.com/friends/family'
于 2019-01-25T15:19:17.280 回答
2

url.resolve isn't what I thought it'd be at first glance... Notice how dir1 is dropped in 2nd example

url.resolve('/one/two/three', 'four')         // '/one/two/four'
url.resolve('http://domain.com/dir1', 'dir2');   // 'http://domain.com/dir2  (dir1 is gone!)

Here's a simple join method I wrote:

    var _s = require('underscore.string');
    /**
     * Joins part1 and part2 with optional separator (defaults to '/'),
     * and adds the optional prefix to part1 if specified 
     * 
     * @param {string} part1
     * @param {string} part2
     * @param {string} [separator] - defaults to '/'
     * @param {string} [prefix] - defaults to empty... pass in "http://" for urls if part1 might not already have it.
     * @returns {string}
     */
    exports.joinWith = function(part1, part2, separator, prefix) {
        var join = "";
        var separatorsFound = 0;

        if( !separator) { separator = "/"; }
        if( !prefix ) { prefix = ""; }

        if( _s.endsWith( part1, separator ) ) { separatorsFound += 1; }
        if( _s.startsWith( part2, separator) ) { separatorsFound += 1; }

        // See if we need to add a join separator or remove one (if both have it already)
        if( separatorsFound === 0 ) { join = separator; }
        else if( separatorsFound === 2 ) { part1 = part1.substr(0, part1.length - separator.length ); }

        // Check if prefix is already set
        if( _s.startsWith( part1, prefix ) ) { prefix = ""; }

        return prefix + part1 + join + part2;
    }

Sample:

// TEST
console.log( exports.joinWith('../something', 'else') );
console.log( exports.joinWith('../something/', 'else') );

console.log( exports.joinWith('something', 'else', "-") );
console.log( exports.joinWith('something', 'up', "-is-") );
console.log( exports.joinWith('something-is-', 'up', "-is-") );

console.log( exports.joinWith('../something/', '/else') );
console.log( exports.joinWith('../something/', '/else', "/") );
console.log( exports.joinWith('somedomain.com', '/somepath', "/") );
console.log( exports.joinWith('somedomain.com', '/somepath', "/", "http://") );

console.log( exports.joinWith('', '/somepath', "/") );

OUTPUT:

../something/else
../something/else
something-else
something-is-up
something-is-up
../something/else
../something/else
somedomain.com/somepath
http://somedomain.com/somepath
/somepath
于 2014-03-27T02:04:07.967 回答