🚀 A powerful CLI tool to generate MVC (Model-View-Controller) structure for Node.js applications with Express.js.
npm install -g express-g-api
npm install express-g-api
npx generate <resource-name> || npx g <resource-name>
generate user
generate product
generate order
generate --help
generate -h
generate --version
generate -v
When you run generate user
, it creates:
project/
├── controllers/
│ └── userController.js
├── routes/
│ └── userRoute.js
├── models/
│ └── userModel.js
└── app.js (if doesn't exist)
The tool automatically creates RESTful API endpoints:
GET /api/v1/users
- Get all usersGET /api/v1/users/:id
- Get single userPOST /api/v1/users
- Create new userPATCH /api/v1/users/:id
- Update userDELETE /api/v1/users/:id
- Delete user
$ npx generate user
✅ Created: controllers/userController.js
✅ Created: routes/userRoute.js
✅ Created: models/userModel.js
✅ Created: app.js
✅ Route auto-imported in app.js
📊 Generation Summary:
Files created: 4
🎉 MVC structure generated successfully!
📁 Resource: User
🌐 API Endpoint: /api/v1/users
Next steps:
1. Install dependencies: npm install express
2. Start server: node app.js
3. Test endpoint: GET http://localhost:3000/api/v1/users
// User Controller
const UserModel = require('../models/userModel');
/**
* Get all users
* @route GET /api/v1/users
*/
exports.getAllUsers = async (req, res) => {
try {
res.status(200).json({
success: true,
message: 'Get all users',
data: []
});
} catch (error) {
res.status(500).json({
success: false,
message: 'Server Error',
error: error.message
});
}
};
// User Routes
const express = require('express');
const router = express.Router();
const {
getAllUsers,
getUser,
createUser,
updateUser,
deleteUser,
} = require('../controllers/userController');
router.get('/', getAllUsers);
router.get('/:id', getUser);
router.post('/', createUser);
router.patch('/:id', updateUser);
router.delete('/:id', deleteUser);
module.exports = router;
- Node.js >= 12.0.0
- npm or yarn
git clone https://github.com/farahmahfouz/express-generate-cli.git
cd express-generate-cli
npm link
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Farah Mahfouz
- GitHub: @farahmahfouz
- Email: farahmahfouz11@gmail.com
If you find this tool helpful, please consider giving it a ⭐ on GitHub!
For issues and feature requests, please use the GitHub Issues page.