gpt4 book ai didi

Node.js 如何获取主机和文件名之间的路径?

转载 作者:搜寻专家 更新时间:2023-11-01 00:10:36 25 4
gpt4 key购买 nike

我正在努力解决这个问题。我想获取主机名和文件名之间的路径:

http://host.com/this/is/what/i/want/file.jpg

方法应该返回“this/is/what/i/want”。

我正在查看 URL 模块,但找不到正确的属性。

url.parse(host).pathname

返回整个路径,但带有文件名:(。

提前致谢

最佳答案

这需要 pathurl内置 Node 模块。

var path = require("path"),
url = require("url"),
myUrl = "http://host.com/this/is/what/i/want/file.jpg",
dir = path.dirname(url.parse(myUrl).pathname);

console.log(dir); // -> /this/is/what/i/want

关于Node.js 如何获取主机和文件名之间的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7188748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com