How to properly modulate code in a Node.JS project?

[TL; DR] Just read the bits in bold .

I am new to Node.JS, but am in the fine building of some work projects. Now I have a burning question that I have been googling for about half an hour, but all I can find is disappointing lightweight tutorials that don't answer my question, so here I go ...

I have a Node.JS project that uses an Express framework. The code for one of the routes now becomes quite large, so my instincts, as a good programmer, should break it into its own class, module or package, or whatever it is called ... and here is my problem. Where is the language tutorial around Node.JS and how to do this by cutting your own code into neatly modulated, easy to read pieces?

It seems that every guide there looks like this ...

  • Create a NodeJS Project
  • Install the npm module in the project
  • Add one route that calls the module
  • Indulge yourself on the back and start applying for the job as a full-stack JavaScript developer.

It makes my head! I want to get deep and dirty with custom-made code, but I also want to do something in a certain standard way, but I don’t know which folders to create, how to enclose them, what naming convention follows or something like that . Where is Jeff's Way for NodeJS?

Please someone call me in the direction of a good textbook or some documentation on this topic so that I can continue my studies. Thank you in advance.

+5
source share
2 answers

In the section on "Organizing Code" on the Absolute Beginner's Guide to Node.JS :

In most applications, your code will be split into several files. Which files do not have any standard or enforcement organization. This is not Rails. There is no concept of views, and controllers go there. You can do whatever you want.

It happened. This is not Rails 😒 You can do whatever you want 😨

But looking at the accepted answer to another question about SO: The folder structure for the Node.js project , we see that the folder named controllers in the root of the project is a common place, but also the routes folder created by using the Express Generator to load a new project considered an alternative to this.

Summary: everything has been reviewed and inspired by both my experience using other MVC frameworks (e.g. Laravel) and @programmingheadache . I am going to save routes , but also create a controllers folder.

Thus, I can save the routes folder specifically for determining the routing of the project and leave the logic separate in several files inside the controllers . I follow the mantra that routing is documentation, and I will hate future reviewers of my project getting bogged down when they just try to establish the early basics of which URIs exist and with which parameters.

0
source

What I usually do is put my logic in the controllers and reference them with route.js. for example: personController with the find (id) method, and then put route.get ('/: id', personController.find) in route.js;

personcontroller:

 module.exports = (app) => { return { find(req, res, next){ const id = req.params.id //return a user } }; 

}

routes:

 router.get('/:id', personController.find); 

pass each error to the next and use the medium to handle them.

Hello

Yang

0
source

Source: https://habr.com/ru/post/1264084/


All Articles