generator-express-layered
v1.0.11
Published
A Yeoman generator for creating express-layered applications
Downloads
28
Readme
generator-express-layered
A Yeoman generator for quickly setting up an Express layered architecture with models, services, controllers, routes, and middleware.
Requirements
Before using this generator, you'll need to have Yeoman installed globally. You can install Yeoman using npm:
npm install -g yoYeoman (yo) is a scaffolding tool that helps you quickly generate code and project structures.
Installation
Once Yeoman is installed, you can install this generator globally with the following command:
npm install -g generator-express-layeredUsage
To generate an Express app structure with a layered architecture, run the generator using:
yo express-layeredThe Generator Prompts
When you run the generator, you will be prompted to provide:
- Project Name: Enter the name of your project. This will be used to create a new directory and name files as needed.
After entering the project name, the generator will scaffold a project structure for you.
What This Generator Does
Project Structure:
server/app.js: The main application entry point.server/server.js: Server setup and initialization.server/routes/api.js: API routes configuration.server/controllers/: Contains controllers for handling route logic.server/services/: Contains services for business logic.server/models/: Folder for model definitions (empty by default).server/middleware/api-response.middleware.js: Middleware for standardized API responses.- Other configuration files like
.gitignoreand placeholder files (.gitkeep) for empty directories.
npm Dependencies:
- Express: A web framework for Node.js.
- Nodemon: For automatically reloading the server during development.
Post-generation:
After scaffolding the project, the generator automatically runs npm install to install all necessary dependencies.
Example Project Structure
After running the generator, your project structure will look similar to this:
project-name/
├── server/
│ ├── app.js
│ ├── server.js
│ ├── routes/
│ │ └── api.js
│ ├── controllers/
│ │ └── base-controller.js
│ ├── middleware/
│ │ └── api-response.middleware.js
│ ├── models/
│ ├── services/
│ │ └── base-service.js
│ └── .gitignore
└── package.jsonCustomizing the Generated Files
Once the generator has run, you can customize the generated files and add your own models, services, and controllers as needed. The generator also includes sub-generators for adding models that will automatically create associated service, controller, and route files.
How the Add-Model Generator Works
You would typically run the add-model sub-generator with:
yo express-layered:add-modelWhat the Add-Model Generator Does
When executed, the add-model sub-generator:
Prompts for model information:
- Model name (e.g., "User", "User-Products")
Generates a complete vertical slice through your layered architecture by creating:
- A model file in the
server/models/directory - A corresponding service in the
server/services/directory - A controller in the
server/controllers/directory - Route definitions in the appropriate route file (extending
server/routes/api.js)
- A model file in the
Follows naming conventions like:
server/models/user.model.jsserver/services/user.service.jsserver/controllers/user.controller.js
Connects the layers by:
- Injecting the model into the service
- Injecting the service into the controller
- Registering the routes in the API router
Generated Files Example
For a "User" model, the generated files might look like:
server/
├── models/
│ └── user.model.js
├── services/
│ └── user.service.js
├── controllers/
│ └── user.controller.js
└── routes/
└── api.js (updated with user routes)
└── user-router.jsThis approach follows the layered architecture pattern, maintaining separation of concerns while automating the repetitive task of creating connected components across different layers of your application.
