nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.69k stars 7.63k forks source link

Best scalable project structure #2249

Closed liebalogh closed 5 years ago

liebalogh commented 5 years ago

[ ] Regression 
[ ] Bug report
[X] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

I have big e-commerce apps, i planning move previous codebase from express (Typescript) to NestJS.

After reading documentation and try create nestjs project, I'm confused with project structure. In my opinion generated project structure not scalable, it strange when large codebase have project structure like that.

Currently on my backend have Controller, Model, Event, Middleware, Validator, interface, static file server (subdomain), websocket, here my current project structure

- src
  - app
    - controller
    - model
    - middleware
    - validator
    - event
  - config
    - server.ts
    - route.ts
    - database.ts
    - ...
  - routes
    - sub
      - static.ts
    - store.ts
    - product.ts
    - ...
  - lib
  - main.ts

I'm not angular developer so i dont know how to manage project structure like that

How i for best project structure on awesome nestjs framework ?

wxs77577 commented 5 years ago

Same question: How to organize project structure ? I need to create a nestjs apis for my frontend UI and backend (admin dashboard) UI, should I create two projects for that or just one (I prefer just one)? AFAIK, I could use just one project. Each module has both frontend and backend apis. Like this:

  1. nest g mo cats to create cats module.
  2. nest g co cats to create cat apis for frontend.
  3. nest g co cats/admin ( or --flat) to create cat apis for backend.
  4. change @Controller('admin') to @Controller('admin/cats') in admin controller file.

That's all what I think. Hope you could give me some advice, thanks a lot.

liebalogh commented 5 years ago

Same question: How to organize project structure ? I need to create a nestjs apis for my frontend UI and backend (admin dashboard) UI, should I create two projects for that or just one (I prefer just one)? AFAIK, I could use just one project. Each module has both frontend and backend apis. Like this:

  1. nest g mo cats to create cats module.
  2. nest g co cats to create cat apis for frontend.
  3. nest g co cats/admin ( or --flat) to create cat apis for backend.
  4. change @Controller('admin') to @Controller('admin/cats') in admin controller file.

That's all what I think. Hope you could give me some advice, thanks a lot.

Thanks for you answer,

In my case frontend and backend are not same server, backend on standalone server and my app will consume it. And my question not about frontend and backend in one project but i need know how i manage scalable files and folder structure on my project

wxs77577 commented 5 years ago

I think you should follow the official documention: just move to modularize from MVC. Think all of your resource as modules:

nest g mo users
nest g co users

nest g mo products
nest g co products

...

It will create the right structure for you.

btw, thanks for your comment, maybe I shouldn't put both frontend and backend apis together.

liebalogh commented 5 years ago

I think you should follow the official documention: just move to modularize from MVC. Think all of your resource as modules:

nest g mo users
nest g co users

nest g mo products
nest g co products

...

It will create the right structure for you.

btw, thanks for your comment, maybe I shouldn't put both frontend and backend apis together.

Oh, now i understand. will i move project structure like this

- src
  - modules
    - user
      - user.controller.ts
      - user.model.ts
    - store
      - store.controller.ts
      - store.model.ts
  - middleware
  - interceptor
  - guard

That's right ?

wxs77577 commented 5 years ago

That's right, but it seems thers is no modules folder.

kamilmysliwiec commented 5 years ago

I need to create a nestjs apis for my frontend UI and backend (admin dashboard) UI, should I create two projects for that or just one (I prefer just one)?

@wxs77577 you can always use monorepo approach - create 2 projects in 1 repo and share common things between them as libraries/packages

@liebalogh without modules directory. Also, you can group common/core things as separate directories + sometimes interceptors/guards/pipes will be scoped by modules. See the following example:

- src
  - core
  - common
    - middleware
    - interceptors
    - guards
  - user
      - interceptors (scoped interceptors)
    - user.controller.ts
    - user.model.ts
  - store
    - store.controller.ts
    - store.model.ts
liebalogh commented 5 years ago

I need to create a nestjs apis for my frontend UI and backend (admin dashboard) UI, should I create two projects for that or just one (I prefer just one)?

@wxs77577 you can always use monorepo approach - create 2 projects in 1 repo and share common things between them as libraries/packages

@liebalogh without modules directory. Also, you can group common/core things as separate directories + sometimes interceptors/guards/pipes will be scoped by modules. See the following example:

- src
  - core
  - common
    - middleware
    - interceptors
    - guards
  - user
      - interceptors (scoped interceptors)
    - user.controller.ts
    - user.model.ts
  - store
    - store.controller.ts
    - store.model.ts

I create global interceptor, that will transform object to response template. And What file should on core folder ?

FSM1 commented 5 years ago

I need to create a nestjs apis for my frontend UI and backend (admin dashboard) UI, should I create two projects for that or just one (I prefer just one)?

@wxs77577 you can always use monorepo approach - create 2 projects in 1 repo and share common things between them as libraries/packages @liebalogh without modules directory. Also, you can group common/core things as separate directories + sometimes interceptors/guards/pipes will be scoped by modules. See the following example:

- src
  - core
  - common
    - middleware
    - interceptors
    - guards
  - user
      - interceptors (scoped interceptors)
    - user.controller.ts
    - user.model.ts
  - store
    - store.controller.ts
    - store.model.ts

I create global interceptor, that will transform object to response template. And What file should on core folder ?

Core would be for example where your actual business logic and rules live.

karocksjoelee commented 5 years ago

I would like Angular with Nestjs in a same repo, the folder both are src . I am thinking a repo with two folder ?

But this cannot manage same package.json together . Anyone has solution ?

kamilmysliwiec commented 5 years ago

@karocksjoelee what about https://www.youtube.com/watch?v=y24fC9Pqr8I

karocksjoelee commented 5 years ago

@karocksjoelee what about https://www.youtube.com/watch?v=y24fC9Pqr8I

umm.. looks great , although I am not going with AngularUniversal , cause I don't want SSR . Good reference of folder structure , thanks man . I will try to put together a starter file for Angular & Nestjs .

sbacem commented 5 years ago

@karocksjoelee for big project is better to separate between Api and Frontend

kamilmysliwiec commented 5 years ago

@sbacem it depends. Either approach has its own pros and cons honestly

vschoener commented 5 years ago

I need to create a nestjs apis for my frontend UI and backend (admin dashboard) UI, should I create two projects for that or just one (I prefer just one)?

@wxs77577 you can always use monorepo approach - create 2 projects in 1 repo and share common things between them as libraries/packages

@liebalogh without modules directory. Also, you can group common/core things as separate directories + sometimes interceptors/guards/pipes will be scoped by modules. See the following example:

- src
  - core
  - common
    - middleware
    - interceptors
    - guards
  - user
      - interceptors (scoped interceptors)
    - user.controller.ts
    - user.model.ts
  - store
    - store.controller.ts
    - store.model.ts

@kamilmysliwiec I'm just asking myself how to properly architecture the project, so far I create all my module in the src:

- src
   - database
       - database.module.ts
   - logger
   - config
   - users
   - auth
  - shared
     - date-manager.ts
     - shared.module.ts

I'm thinking about moving the database/logger module inside a core module and maybe the Date manager as well but for the last, I'm not sure, I think it's better to let it in a shared module containing helpers for example... But is could also live in the core one,

What do you think? What would be the "best" way to architecture it?

SylvSylv commented 4 years ago

I have a question concerning the module's folder structure :

Let's say I have a BasePet module. Then a Cat module which extends BasePet, but with it's own service and controller, same for a Dog module which extends BasePet.

Would you suggest to encapsulate even more the module's folders ?

HackPoint commented 4 years ago

@kamilmysliwiec I'm just asking myself how to properly architecture the project, so far I create all my module in the src:

- src
   - database
       - database.module.ts
   - logger
   - config
   - users
   - auth
  - shared
     - date-manager.ts
     - shared.module.ts

If you will use libs, it will be much simple to separate the application logic etc:

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.