dtgr3392 2014-01-17 08:44
浏览 27

如何使用Express.js构建宁静的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条回答 默认 最新

  • duanjiwu0324 2014-01-18 05:05
    关注

    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 :) That's just the way you would think about it.

    GoodLuck();

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度