Bigger projects Node.js and RESTful API

2019-07-04 19:26发布

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.

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.

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:

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.

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.

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.

1条回答
你好瞎i
2楼-- · 2019-07-04 19:38

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

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

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 :)

查看更多
登录 后发表回答