0

Wondering what is the best way to structure the ejs part of the application, here is my current structure.

inside maindir/

  • index.js // starts the application
  • node-modules
  • homepage/ // i would be creating a folder like this for each view
  • partials/ // has common components navbar.ejs and footer.ejs.

inside the maindir/homepage/

  • routes.js // has the app.get/post/put/deletes({"/homepage", controller.*})'s
  • controller.js // exports the controllers
  • models.js // exports mongo models
  • ejs/

inside the /maindir/homepage/ejs/

  • app.ejs // imports common partials e.g. navbar & footer from partials/ & homepage specific components from components/
  • components/

is this a decent way to structure an application? specifically the importing from the partials/ directory into each new view or is there a better way to achieve this goal ?

2 Answers 2

1

is this a decent way to structure an application?

The question/answer is entirely subjective.

My answer will be based on what most developers do.

Traditionally, the root folder structure of an Express application is the following:

example
├── app.js
├── controllers
├── models
├── public
└── views

As you can see this is the MVC pattern. An alternative pattern to the above, which is common in frontend applications, is organizing it by feature/components:

example
├── app.js
├── auth
│   ├── auth-controller.js
│   ├── auth-view.ejs
│   └── auth-model.js
├── notification
│   ├── notification-controller.js
│   ├── notification-view.ejs
│   └── notification-model.js
└── topic

It boils to your personal preference or what your team decides.

Sign up to request clarification or add additional context in comments.

1 Comment

First is good, however controllers is often renamed to routes. I don't prefer second.
0

Using express-generator can be a good starting point if you are new to setting up an express app.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.