Ren*_*ama 101 url string-concatenation node.js
require("path").join用于连接URL 是否安全,例如:
require("path").join("http://example.com", "ok"); 
//returns 'http://example.com/ok'
require("path").join("http://example.com/", "ok"); 
//returns 'http://example.com/ok'
如果没有,如果不编写完整的ifs代码,你会采用什么方式做这件事.
Mat*_*tis 118
号   path.join()与URL中使用时,将返回不正确的值.
这听起来像你想要的url.resolve.来自Node文档:
url.resolve('/one/two/three', 'four')         // '/one/two/four'
url.resolve('http://example.com/', '/one')    // 'http://example.com/one'
url.resolve('http://example.com/one', '/two') // 'http://example.com/two'
编辑: 
正如安德烈斯在评论中正确指出的那样,url.resolve只有在问题与示例一样简单的情况下才会有所帮助.  url.parse也适用于这个问题,因为它通过URL对象返回一致且可预测的格式化字段,从而减少了"代码充满ifs"的需要.
sto*_*one 39
不,您不应该使用path.join()加入URL元素.
现在有一个包这样做的包.因此,不是重新发明轮子,编写所有自己的测试,查找错误,修复它们,编写更多测试,找到不起作用的边缘情况等等,您可以使用此包.
https://github.com/jfromaniello/url-join
npm install url-join
var urljoin = require('url-join');
var fullUrl = urljoin('http://www.google.com', 'a', '/b/cd', '?foo=123');
console.log(fullUrl);
打印:
' http://www.google.com/a/b/cd?foo=123 '
Ikb*_*bel 19
Axios有一个辅助函数,可以组合 URL。
function combineURLs(baseURL, relativeURL) {
  return relativeURL
    ? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '')
    : baseURL;
}
来源:https : //github.com/axios/axios/blob/fe7d09bb08fa1c0e414956b7fc760c80459b0a43/lib/helpers/combineURLs.js
const nodeUrl = require('url')
const nodePath = require('path')
> const myUrl = new nodeUrl.URL('https://example.com')
pathname=和path.join构造任何可能的组合:> myUrl.pathname = nodePath.join('/search', 'for', '/something/')
'/search/for/something/'
(你可以看到path.join争论是多么自由)
> myUrl.toString()
'https://example.com/search/for/something/'
此技术使用内置库。在 CVE、维护等方面,第三方依赖越少越好。
没有什么比标准库更能证明或更好地测试的了。
当我查看代码时,我坚持从不手动将 URL 作为字符串操作。一方面,看看规范有多复杂。
其次,尾随/前缀斜杠 ( /)的缺失/存在不应导致一切中断!你永远不应该这样做:
const url = `${baseUrl}/${somePath}`
尤其不是:
uri: host + '/' + SAT_SERVICE + '/' + CONSTELLATION + '/',
其中我见过。
在WHATWG URL对象的构造函数有一个(input, base)版本,并且input可以使用相对/,./,../。结合它,path.posix.join你可以做任何事情:
const {posix} = require ("path");
const withSlash = new URL("https://example.com:8443/something/");
new URL(posix.join("a", "b", "c"), withSlash).toString(); // 'https://example.com:8443/something/a/b/c'
new URL(posix.join("./a", "b", "c"), withSlash).toString(); // 'https://example.com:8443/something/a/b/c'
new URL(posix.join("/a", "b", "c"), withSlash).toString(); // 'https://example.com:8443/a/b/c'
new URL(posix.join("../a", "b", "c"), withSlash).toString(); // 'https://example.com:8443/a/b/c'
const noSlash = new URL("https://example.com:8443/something");
new URL(posix.join("./a", "b", "c"), noSlash).toString(); // 'https://example.com:8443/a/b/c'
如果您使用 Angular,则可以使用Location:
import { Location } from '@angular/common';
// ...
Location.joinWithSlash('beginning', 'end');
但仅适用于 2 个参数,因此您必须链接调用或编写辅助函数来执行此操作(如果需要)。
没有!在Windows path.join上将加入反斜杠.HTTP网址始终是正斜杠.
怎么样
> ["posts", "2013"].join("/")
'posts/2013'
小智 6
当我尝试使用PATH来连接url部分时,我遇到了问题.
PATH.join条纹'//'向下'/'这样会使绝对网址无效(例如http:// ...  - > http:/ ...).对我来说,快速修复是:
baseurl.replace(/\/$/,"") + '/' + path.replace(/^\//,"") )
或者由Panic上校发布的解决方案:
[pathA.replace(/^\/|\/$/g,""),pathB.replace(/^\/|\/$/g,"")].join("/")
我们这样做:
var _ = require('lodash');
function urlJoin(a, b) {
  return _.trimEnd(a, '/') + '/' + _.trimStart(b, '/');
}
| 归档时间: | 
 | 
| 查看次数: | 45145 次 | 
| 最近记录: |