[英]Structuring a Nuxt 3 server directory
Context语境
I am creating an app which uses multiple server api points to perform different functions such as writing to Firebase Firestore.我正在创建一个应用程序,它使用多个服务器 api 点来执行不同的功能,例如写入 Firebase Firestore。 These api points are set up in the server directory like so:
这些 api 点在服务器目录中设置如下:
server/
api/
foo.ts
bar.ts
lib/
firebaseConfig.ts
firebaseHelpers.ts
The two api endpoints under api/
import helper methods (such as writing to and reading from the database) from the firebaseHelpers.ts
file. api/
下的两个 api 端点从firebaseHelpers.ts
文件导入辅助方法(例如写入和读取数据库)。 The firebaseHelpers.ts
file imports the Firebase configuration from the firebaseConfig.ts
file. firebaseHelpers.ts
文件从firebaseConfig.ts
文件导入 Firebase 配置。
Question问题
My goal is to separate the endpoints, helper-functions and config files from each other.我的目标是将端点、辅助函数和配置文件彼此分开。 Also, the helper-functions and config files only need to be available in the server.
此外,辅助函数和配置文件只需要在服务器中可用。
Considering this, my question is if this is a good structure to use in Nuxt?考虑到这一点,我的问题是这是否是一个适合在 Nuxt 中使用的结构? I have seen others use the
plugins/
and utils/
directories, is that how I should divide my lib/
directory in my case?我看到其他人使用
plugins/
和utils/
目录,在我的情况下我应该如何划分lib/
目录? Is it okay to use a lib/
directory like this?这样使用
lib/
目录可以吗?
If you want to take full advantage of nuxt's auto-import feature, then you can use the utils
folder or use plugins
to create your server-side firebase plugin.如果你想充分利用 nuxt 的自动导入功能,那么你可以使用
utils
文件夹或使用plugins
来创建你的服务器端 firebase 插件。 Creating a lib
folder also makes sense.创建一个
lib
文件夹也很有意义。 Optionally, you can structure your lib
folder this way: /lib/firebase
, /lib/stripe
, etc.或者,您可以这样构造您的
lib
文件夹: /lib/firebase
、 /lib/stripe
等。
I'm also building an app with Nuxt3 and Supabase, and this is the exact problem that I'm facing.我也在用 Nuxt3 和 Supabase 构建一个应用程序,这正是我面临的问题。 In my case, I have the following server structure:
就我而言,我有以下服务器结构:
/api
to the path /api
repositories
as per Domain-driven Design standardsrepositories
Ultimately, you decide how you want to structure your project.最终,您决定如何构建项目。 You can take inspiration from other frameworks such as Ruby on Rails or learn Domain-driven Design Architecture.
您可以从其他框架中获取灵感,例如 Ruby on Rails 或学习领域驱动设计架构。 However, I recommend not to overthink it so you don't end up creating unnecessary abstractions.
但是,我建议不要想太多,这样您就不会最终创建不必要的抽象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.