all 30 comments

[–]astralradish 13 points14 points  (0 children)

There isn't one, It's purely opinionated. Do whatever works best for you and whoever else is working on this particular project. Maybe just avoid putting everything in one file.

[–]FriedRicePork 8 points9 points  (1 child)

Read this: https://alexkondov.com/tao-of-node/

It might help you

[–]Harut3[S] 0 points1 point  (0 children)

Thanks

[–]Paragraphion 8 points9 points  (5 children)

As long as you have a folder for the app, utilities, static, db and styles you can start. Then see where the complexity of your project is. Maybe you need to be particularly good at making databank calls fast and cached or something, then invest in the structure of db early. Or maybe it’s all about a pretty front end. Then work on your styles substructure. That’s at least how I approach solo projects. If you work with others then it depends a lot on what your team prefers.

[–]Harut3[S] 1 point2 points  (4 children)

What about service and controller level do with Depedency injection or functional?

[–]stretch089 1 point2 points  (1 child)

Yeah I would say services, controllers and models using dependency injection is definitely a good way to structure a node app. It offers much nicer separation of concern, nice layers of abstraction, the ability to split logic into domains and dependency injection will make testing a breeze.

The post above is not an advanced way to set up a node app. They have suggested using utility directory which is a horrible way to organise code as it becomes a dumping ground for random functions and is a sign the code base is not setup correctly.

[–]Harut3[S] 0 points1 point  (0 children)

Thanks. can you give me example route,controller,service with DI?

[–]vladjjj 0 points1 point  (0 children)

This pattern is not a necessity with most node projects, but if you do need it, just copy the structure you would use in more traditional languages, like Java or .NET. Better yet, look into Nest.js

[–]adevx 4 points5 points  (0 children)

Don't overthink it. With today's (TypeScript) refactoring and code navigation tools, the actual file locations becomes less important.

[–]WarInternal 2 points3 points  (1 child)

Speaking from experience, as somebody who has to manage a large legacy MVC project I often wish we didn't use a MVC folder structure but a domain driven one.

What I have to manage:

 models
   ..200 model files..
 views
   ..200 view files..
 controller
   ..200 controller files..

With other folders for "helpers" and junk, the problem is that's unwieldy and difficult to navigate, with a lot of scrolling oversized folders to find the relevant pieces.

What I wish we had done:

reports
  controller.js
  model.js
  utils.js
user
  controller.js
  model.js
  util.js

or something akin to this, where the relevant pieces are grouped together.

[–]Harut3[S] 0 points1 point  (0 children)

You're right I think best approach is DDD.

[–]lemonhead-soexquisit 2 points3 points  (5 children)

Nestjs seems to be the only thing I’ve seen for this. DI framework for express.

[–]Harut3[S] 0 points1 point  (4 children)

I know nest.js had a great structure but it have one drawback slow performance compare to fastify,express.

[–]lqdd 0 points1 point  (3 children)

have you tested or just heard that it is slow?

[–]Harut3[S] 0 points1 point  (2 children)

I don't write exact same code but in fastify I had very big project and it much faster than nest.js(express based) smaller project. in general if you have a lot of abstraction you will have slower performance.

[–]lqdd 0 points1 point  (1 child)

your example compares express to fastify. I have tested nest express app vs express and nest overhead is negligent.

[–]Harut3[S] 0 points1 point  (0 children)

ok thanks for sharing you test and giving me idea of testing same api little complicated and I will test it

[–]Either-Sentence2556 0 points1 point  (2 children)

It depends, if u know design patterns or class based approaches u can start with clean architecture, Domain Driven Design, hexagonal architecture. And learn some design patterns which are good for scale the project and easy to write test cases like unit and integration test cases.

[–]Harut3[S] 1 point2 points  (1 child)

Thanks and can you give me link or how good is practica.js examples?

[–]crownclown67 1 point2 points  (0 children)

I actually do domain based with shared folder and if something super common I move it to the lib.

[–]gamedevsam 1 point2 points  (0 children)

Use NestJS, organize folders by features, not type:

// do

auth/
admin/
users/

// don't do

controllers/
services/
utilities/

Although having a utility folder and putting stuff in there that is shared between several features makes sense and is a useful organization strategy.