更大的项目 Node.js 和 RESTful API [英] Bigger projects Node.js and RESTful API

查看:19
本文介绍了更大的项目 Node.js 和 RESTful API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 node.js,它看起来确实是一个非常好的环境.我使用过很多不同的技术和服务器,主要是 php 和 Java (jsp),但也涉足了一些 RoR 和 Python.

I'm looking into node.js which really seem like a pretty nice environment. I've worked with a lot of different Technologies and for server, mainly php and Java (jsp), but dabbled in som RoR and Python.

我发现 node.js 真的很容易上手和运行,使用起来感觉很自然,而且我发现了一些很好的入门级教程.

I find node.js really easy to get up and running and it feels quite natural to work with, and I found some good entry level tutorials.

我只是缺少一些中间资源.例如,在创建更大的框架或 API 时,您将如何构建或构建它.我设置了一些较小的 api 来尝试它会像这样:

I just am missing some more intermediate resources. For example when creating bigger frameworks or api's how would you structure or architect it. I set up some smaller api's to try it out where it would go something like this:

我使用了 Express 框架来创建一个 http 服务器,监听一个端口,设置一个 express 对象并绑定一些请求.

I've made use of the Express framework to create a http server, listen to a port, set up an express object and bound some requests.

然而,这些已经很小,目的是学习,如果我考虑扩大生产 API 的大小,也许还想做其他事情,比如提供网页服务.我发现很难看出架构会是什么样子.

However these have been quite small, and the purpose has been learning, if I think about scaling up the size of the API for production, perhaps wanting to do other stuff like serve web-pages as well. I find it hard to see how the architecture would look.

这很模糊,因为我还是 node.js 的新手,但我主要考虑的是,如果您通常将所有 api 保存在一个文件中,或者是否有将其拆分为模块的好方法?如果有人知道更多关于如何在 node.js 中工作时设计架构的资源

It's vague as I am still new to node.js but I'm mainly thinking about things like if you typically keep all api in one file or if there are good ways to split it up into modules? And if anyone know any resource talking a bit more about how to design the architecture when working in node.js

抱歉我的问题含糊不清,感谢您的阅读.

Sorry for the vague question and thanks for reading.

推荐答案

在我看来,如果您想构建复杂或大型的 API,Express 是一个不错的选择.

In my opinion, Express is the good way to go if you want to build complex or big APIs.

它很容易测试(例如使用 Mocha 或 Jasmine)和可定制,特别是由于它的 中间件.

It is among others easily testable (for instance with Mocha or Jasmine) and customizable, especially thanks to its middlewares.

对于目录结构,我通常使用的是(至少)以下内容:

For the directory structure, what I usually use is (at least) the following:

  • app.js :主要入口点.将创建 express 应用程序,指示每个路由前缀使用哪个控制器,并分配中间件.之前项目的示例
  • controllers :将包含控制器,处理请求的函数,与标准 MVC 框架(例如 UserController,...)中的风格相同.每个控制器都会创建一个 express Router 对象并将其导出.在控制器内部,各个处理程序负责各个 API 请求,例如 /api/users/list.它会使用一些库来访问您的数据(例如 Mongoose for MongoDB),然后将响应发送到客户端.示例(UserController.js)
  • models :将包含模型及其所有属性和方法.就我而言,它将是猫鼬模型.示例(Song.js)
  • middlewares :将包含项目的各种中间件.一个实际的例子是中间件检查传入请求中的访问令牌,如果没有,则返回 403 HTTP 错误.示例 (AuthMiddleware.js)
  • helpers:各种帮手
  • 测试:对您的 API 进行单元测试
  • app.js : the main entrypoint. Will create the express application, indicate which controller to use for every route prefix, and assign the middlewares. Example from a previous project
  • controllers : will contain the controllers, the functions which will handle the requests, in the same style as in standard MVC frameworks (e.g. UserController, ...). Each controller would create an express Router object and export it. Inside the controllers, individual handlers are in charge of individual API requests, such as /api/users/list. It would use some library to access your data (e.g. Mongoose for MongoDB), and would then send the response to the client. Example (UserController.js)
  • models : will contain the models with all their attributes and methods. In my case, it would be the Mongoose models. Example (Song.js)
  • middlewares : will contain the various middlewares of the project. A practical example would be a middleware checking for an access token in the incoming request, and returning a 403 HTTP error if not. Example (AuthMiddleware.js)
  • helpers : various helpers
  • tests : unit tests of you API

这可能是最小的目录组织.最重要的是,您可能希望使用 EJS 等模板引擎来提供网页服务.看看 « 使用 EJS 来模板化你的节点应用程序 ».

This could be the minimal directory organization. On top of that, you may want to use a templating engine such as EJS to serve webpage. Take a look at « Use EJS to template your node application ».

这只是为了让您大致了解快速目录结构的外观,但当然还有很多(更好?)其他可能性.希望能给你一个快速而有用的见解:)

This is only to give you an overview of what an express directory structure could look like, but there are of course plenty (better?) other possibilities. Hope that gives you a quick and useful insight :)

这篇关于更大的项目 Node.js 和 RESTful API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆