Nest: Best scalable project structure

Created on 22 May 2019  Â·  17Comments  Â·  Source: nestjs/nest


[ ] 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 ?

needs triage question 🙌

Most helpful comment

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

All 17 comments

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.

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

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.

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 ?

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

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 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 ?

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.

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 ?

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

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

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

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?

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 ?

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

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rlesniak picture rlesniak  Â·  3Comments

cojack picture cojack  Â·  3Comments

hackboy picture hackboy  Â·  3Comments

rafal-rudnicki picture rafal-rudnicki  Â·  3Comments

marshall007 picture marshall007  Â·  3Comments