Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best scalable project structure #2249

Closed
liebalogh opened this issue May 21, 2019 · 17 comments
Closed

Best scalable project structure #2249

liebalogh opened this issue May 21, 2019 · 17 comments
Labels
needs triage This issue has not been looked into type: question 🙌

Comments

@liebalogh
Copy link

liebalogh commented May 21, 2019


[ ] 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
Copy link

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
Copy link
Author

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
Copy link

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
Copy link
Author

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
Copy link

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

@kamilmysliwiec
Copy link
Member

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
Copy link
Author

liebalogh commented May 22, 2019

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
Copy link
Contributor

FSM1 commented May 22, 2019

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
Copy link

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

  • server-src
    • src ( nestjs )
  • angular-src
    • src ( angular )

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

@kamilmysliwiec
Copy link
Member

@karocksjoelee
Copy link

@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
Copy link

sbacem commented May 24, 2019

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

@kamilmysliwiec
Copy link
Member

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

@BrunnerLivio BrunnerLivio added the needs triage This issue has not been looked into label Aug 8, 2019
@vschoener
Copy link

vschoener commented Oct 28, 2019

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
Copy link

SylvSylv commented Nov 19, 2019

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
Copy link

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

  • auth-lib
  • shared
    • api-lib
      -core
      and so on...

@lock
Copy link

lock bot commented Apr 15, 2020

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.

@lock lock bot locked as resolved and limited conversation to collaborators Apr 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into type: question 🙌
Projects
None yet
Development

No branches or pull requests

10 participants