Czy jest to bezpieczne do require("path").joinłączenia adresów URL, na przykład:
require("path").join("http://example.com", "ok");
//returns 'http://example.com/ok'
require("path").join("http://example.com/", "ok");
//returns 'http://example.com/ok'
Jeśli nie, w jaki sposób możesz to zrobić bez pisania pełnego kodu ifs?
path.posix.join('/one/two/three', 'four') // '/one/two/three/four, path.posix.join('/one/two/three/', 'four') // '/one/two/three/four,path.posix.join('/one/two/three/', '/four') // '/one/two/three/four
path.posix.join('http://localhost:9887/one/two/three/', '/four'), złączenie pozbywa się jednego z podwójnych cięć whttp://
'http://localhost:9887/one/two/three/'.replace(/^\/+|\/+$/, '') + '/' + '/four'.replace(/^\/+|\/+$/, '')i możesz to zrobić, String.prototype.trimSlashes = function() { return this.replace(/^\/+|\/+$/, ''); }jeśli nie chcesz w kółko wpisywać wyrażenia regularnego. stackoverflow.com/a/22387870/2537258
['http://localhost:9887/one/two/three/', '/four'].map((part) => part. replace(/^\/+|\/+$/, '')).join('/')