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

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

问题描述

我正在研究node.js,它看起来确实是一个非常不错的环境.我曾经使用过许多不同的技术,也曾使用过很多服务器技术,主要是php和Java(jsp),但还涉猎过Som 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 : 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天全站免登陆