How to structure code for a restful API with Express.js?

huangapple go评论96阅读模式
英文:

How to structure code for a restful API with Express.js?

问题

最近,我一直在使用Sails.js构建一个REST API。我确定了我应用程序所需的资源,并观察到(在我看来)大多数基于Express构建的框架在操作单个资源时非常有效。

此外,我发现在大多数请求中,我需要操作多个资源。例如,假设我们有以下结构和资源之间的链接:

用户 -- 用户元数据
      `- 图书 -- 图书元数据
               `- 作者
      
      `- 图库 -- 图片 -- 图片元数据
                 `- 视频 -- 视频元数据

上述资源包括:用户图书图库图片视频。每个资源只存储其子资源的ID。
我在几乎所有情况下遇到的问题是在插入新的子资源时。在插入一本书之后,我需要找到它的所有者并添加对这本书的引用。

当我上传一张图片时,情况变得更糟。在这种情况下,创建一个图片后,通过知道上传图片的用户,我创建一个图库,将图片引用推送到图库,并将图库的ID分配给该用户。

问题在于我需要从用户控制器、图片控制器或其他控制器中执行此操作并操作所有资源。这样一来,代码重复性增加,事情变得混乱和丑陋(例如在图片控制器或图库控制器中调用用户模型)。

当然,最简单的方法是摆脱图库子资源,直接将视频和照片添加到用户中。但这只是一个例子。

因此,我希望通过拥有这4个单独的模块(用户、图库...)来构建我的应用程序,它们将提供它们的API(即使是基本的,例如CRUD)。然后,将路由请求分派到适当的控制器/处理程序,该控制器/处理程序将与我的模块进行交互并操作它们。此外,当请求涉及单个资源时,我希望自动分派到模块的处理程序(绕过控制器/处理程序)。

这种模式对于扩展REST API(尽管与REST无关,但与数据操作有关)是否有效?

如果有效,是否有任何库甚至是Express可以用来创建这些模块?我在模型中使用mongoDb和mongoosejs。

你能为我推荐一个应用程序的文件结构吗?

我还在这个问题中标记了golang,因为我希望找到一个适合我的需求的框架(也许是beego、martini或goweb)。

我不打算提出一个主观的问题,但我希望有人已经对此有一些经验,我已经厌倦了为解决方案搜索,我得到的所有结果都是如何使用我不知道什么框架构建基本的REST API,并没有真正解决构建一个可用于生产的重型应用程序的架构/可扩展性。

谢谢。

英文:

Recently I've been working on building a REST API using Sails.js I identified the resources I need for my app and what I observed (IMO) is that the majority of frameworks (built on top of Express) are very effective when it comes to manipulating a single resource.

Also, I found that in majority of requests, I needed to manipulate more than a resource. So for example let's say that we have the following structure and the links between resources:

Users -- user's metadata
      `- Books -- book metadata
               `- author
      
      `- Gallery -- Images -- image metadata
                 `- Videos -- video metadata

The resources from above are: Users, Books, Gallery, Images and Videos. Every resource will store only id's of it's subresources.
The problem I encountered in almost all cases was when I inserted a new subresource. After inserting a book then I would find it's owner and add a reference to this book.

Things gets worse when I upload an image. In this case after creating an Image, by knowing the user who uploaded the image, I create a gallery, push the image reference in gallery and assign the gallery's id to that user.

The problem here is that I need to to this from the users controller, or from images controller or whatever controller and manipulate all the resources. Like this, the code repetition gets higher and things becomes mixed and ugly (having for example calls to Users model from Images controller or Gallery controller).

Of course the simplest way would be to get rid of the Gallery subResource and add the videos and photos right to the user. But this was just an example.

So I would like to structure my app by having those 4 separate modules (Users, Gallery ...) which will provide their API (even if it's a basic one, i.e. CRUD). Then dispatch the routed request to the appropriate controller/handler which will interact with my modules and manipulate them. Also, when the request implies a single resource, I would like to automatically dispatch to module's handler (shortcutting the controller/handler).

Is this a good pattern for scaling a REST API (although this has nothing to do with the REST, but with data manipulation)?

If so, is there any library or even Express that I can use to create the modules? I use mongoDb with mongoosejs for models.

Can you recommend me a file structure for the app?

I also tagged golang to this question because I'm interested in finding is there any framework that would fit my needs (maybe beego, martini or goweb).

I don't intend to ask an opiniated question, but I rather hope that someone already have some experience with this and I'm tired of googling for a solution, all the results I'm gettint are how to build a basic rest api with I don't know what framework and doesn't really address the architecture/scalability of obtaining a heavy app production ready.

Thanks.

答案1

得分: 1

我自己对sails.js也不太熟悉,根据我所了解的,你似乎在寻找关联模型。

尽管它们尚未完全实现,但你仍然可以在sails v0.10.0中使用它们。

我认为在你的项目准备好投入生产时,关联模型应该已经完全实现了。

你可以在这里查看它们:https://github.com/balderdashy/sails/issues/124

这意味着你可以这样说:

  • 视频属于画廊1

  • 图片属于视频9

  • 画廊拥有多个视频和多个图片

你实际上不需要编写这些代码 How to structure code for a restful API with Express.js? 这只是你思考的方式。

祝你好运!

英文:

I'm new to sails.js myself, from what I know it seems like you're actually looking for associative models.

They haven't been fully implemented yet, but you can still use them with sails v0.10.0

I should think that associations would be fully implemented by the time your project is ready for production.

You can check them out here: https://github.com/balderdashy/sails/issues/124

This means that you say stuff like this

  • Video belongsTo Gallery 1

  • Picture belongsTo Video 9

  • Gallery hasMany Video and hasMany Picture

You don't actually write that How to structure code for a restful API with Express.js? That's just the way you would think about it.

GoodLuck();

huangapple
  • 本文由 发表于 2014年1月17日 16:44:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/21181386.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定