简体   繁体   English

构建 Nuxt 3 服务器目录

[英]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:就我而言,我有以下服务器结构:

  1. middleware - nuxt middlewares中间件 - nuxt中间件
  2. routes - works the same as api but without the need to prepend /api to the path routes - 与 api 相同,但不需要在路径前加上/api
  3. services - supabase DB queries.服务 - supabase 数据库查询。 This can also be renamed as repositories as per Domain-driven Design standards这也可以根据领域驱动设计标准重命名为repositories
  4. utils - small helper functions (eg password hashing, data coercion, etc.) utils - 小辅助函数(例如密码散列、数据强制等)
  5. validations - where I validate the query params / body params for each route验证 - 我在其中验证每条路线的查询参数/正文参数

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM