gpt4 book ai didi

javascript - 路径元素的命名约定

转载 作者:行者123 更新时间:2023-11-30 06:14:08 25 4
gpt4 key购买 nike

我实际上正在寻找一个好的路径约定,主要是在元素之间存储 / 的位置:

  • 在目录的末尾 const dir = "example/"
  • 在以下元素的开头 const file = "/example1.md"
  • 都不是(允许使用Array.join('/'))

我正在查看 URL convention试图找到一个好的做法,但它没有谈论路径。

示例

让我们看一个真实的例子

const base_url = process.env.API_URL // http://localhost:3000(/)
const endpoint_path = "(/)users/auth(/)"
const endpoint_name = "(/)tfa-validation"

// Slash before of after
let path = base_url + endpoint_path + endpoint_name;
// Or without slash
path = `${base_url}/${endpoint_path}/${endpoint_name}`;
path = [base_url, endpoint_path, endpoint_name].join('/')

约定是否已经存在?解决方案有什么优势吗?

最佳答案

我认为没有任何约定。所以,我的意见是:

  1. 不要在端点路径或名称中使用“/”。保持它们干净,因为您可能希望单独使用这些变量;
  2. 使用 join 函数,因为它更干净。

const base_url = process.env.API_URL // http://localhost:3000
const endpoint_path = "users/auth"
const endpoint_name = "tfa-validation"

path = [base_url, endpoint_path, endpoint_name].join('/')

关于javascript - 路径元素的命名约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57257860/

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