在Windows上的NodeJS上使用path.join创建URL
我有两个动态的URL片段,我正试图将它们连接在一起,形成一个完整的URL。由于我不知道要连接的确切字符串,我想使用路径连接库来避免字符串连接错误,如在Windows上的NodeJS上使用path.join创建URL,windows,node.js,url,path,Windows,Node.js,Url,Path,我有两个动态的URL片段,我正试图将它们连接在一起,形成一个完整的URL。由于我不知道要连接的确切字符串,我想使用路径连接库来避免字符串连接错误,如“http://www.mysite.com/friends//12334.html“,它有一个额外的斜杠等 我正在使用Node.js在Windows 7家用计算机上工作 我尝试使用路径库的路径。join(…),但由于我在Windows上,它将所有的前斜杠向后翻转,这对于URL显然是不正确的。例如: var path = require('path'
“http://www.mysite.com/friends//12334.html“
,它有一个额外的斜杠等
我正在使用Node.js在Windows 7家用计算机上工作
我尝试使用路径
库的路径。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样式的分隔符?URL不是文件系统路径,因此path
中的任何内容都不适用于您的要求。如果满足您的需要,我建议使用url.resolve()
,否则建议使用url.format()
。请注意,您不能在代码中简单地将这两个函数替换为path.join()
,因为它们需要不同的参数。仔细阅读文档。url.resolve
不是我第一眼看到的。。。注意,在第二个示例中,dir1是如何被删除的
url.resolve('/one/two/three', 'four') // '/one/two/four'
url.resolve('http://domain.com/dir1', 'dir2'); // 'http://domain.com/dir2 (dir1 is gone!)
下面是我写的一个简单的连接方法:
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;
}
样本:
// 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', "/") );
输出:
../something/else
../something/else
something-else
something-is-up
something-is-up
../something/else
../something/else
somedomain.com/somepath
http://somedomain.com/somepath
/somepath
…如何获取path.join以强制使用UNIX样式的分隔符而不是Windows样式的分隔符
虽然(正如@ebohlman所指出的)URL不是文件系统路径,但可以用来创建URL.pathname
组件
const path=require('path');
const url=require('url');
常数原点http://www.example.com/';
const pathname=path.posix.join(path.posix.sep,'friends','family');
//突然。。。
const myURL=新的URL(路径名、来源);
console.log(myURL.href);
// => 'http://www.example.com/friends/family'
//循序渐进。。。
const myURL2=新URL(源);
console.log(myURL2.href);
// => 'http://www.example.com/'
myURL2.pathname=路径名;
console.log(myURL2.href);
// => 'http://www.example.com/friends/family'
啊,谢谢。我知道url
模块,但没有意识到url.resolve
会将url连接在一起。我认为path
可能是下一个最好的选择。明智地说,url.resolve不是path.join的替代品,而是url的替代品。相反,它“采用一个基本URL和一个href URL,并像浏览器解析锚定标记一样解析它们。”。特别注意,如果你给它“”和“2”,它会给你“”而不是“”