putitdigital-api-express
v0.1.3
Published
Express API scaffolding package that auto-generates folder structure based on resource.json
Downloads
1,974
Maintainers
Readme
putitdigital-api-express
A comprehensive Express.js API scaffolding package that automatically generates a complete folder structure with authentication, database configuration, and boilerplate files based on a resource.json configuration file.
Installation
npm install putitdigital-api-expressHow it works
When installed, this package runs a postinstall script that automatically:
- Checks for a
resource.jsonfile in your project root - Creates template files for
.env,.request.rest, andserver.js - If
resource.jsoncontains a"tables"key, scaffolds the complete API structure with:- Models with database queries
- Controllers for business logic
- Routes for HTTP endpoints
- Authentication middleware
- Database configuration
- Express server setup
Quick Start
Step 1: Install the package
npm install putitdigital-api-expressThe postinstall script will automatically create template files in your project root.
Step 2: Create or update resource.json
Define your API resources by table name:
{
"tables": {
"users": {},
"posts": {},
"comments": {}
}
}Step 3: Configure your environment
Edit the generated .env file with your database credentials:
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=yourpassword
DB_NAME=yourdb
JWT_SECRET=your_secret_key_here
PORT=3000Step 4: Run the scaffolding
Simply reinstall to trigger scaffolding:
npm installOr manually run:
node node_modules/putitdigital-api-express/lib/scaffold.jsStep 5: Start your server
node server.jsGenerated folder structure
project-root/
├── resource.json # API configuration
├── .env # Environment variables
├── .request.rest # REST client requests
├── server.js # Express app entry point
├── API/
│ ├── models/
│ │ ├── usersModel.js
│ │ ├── postsModel.js
│ │ └── commentsModel.js
│ ├── controllers/
│ │ ├── usersController.js
│ │ ├── postsController.js
│ │ └── commentsController.js
│ ├── routes/
│ │ ├── usersRoutes.js
│ │ ├── postsRoutes.js
│ │ └── commentsRoutes.js
│ ├── middleware/
│ │ └── authMiddleware.js
│ └── config/
│ └── db.js # Database connection pool
└── node_modules/
└── putitdigital-api-express/Features
- ✅ Auto-scaffolding - Generates complete API structure on install
- ✅ Authentication - Built-in JWT-based authentication middleware
- ✅ Password Hashing - Bcrypt integration for secure password storage
- ✅ Database Layer - MySQL2 connection pooling for MySQL/MariaDB
- ✅ Environment Variables - dotenv support for configuration
- ✅ Models & Controllers - MVC pattern boilerplate
- ✅ Express Routes - RESTful endpoint structure
- ✅ Cookie Parser - Middleware for parsing cookies
- ✅ Safe Generation - Skips files that already exist
- ✅ Idempotent - Safe to run multiple times
Generated Endpoints
For each table in resource.json, the following RESTful endpoints are generated:
GET /api/{table}- List all recordsGET /api/{table}/:id- Get record by IDPOST /api/{table}- Create new recordPUT /api/{table}/:id- Update recordDELETE /api/{table}/:id- Delete record
Example: Users table
GET /api/users - Fetch all users
GET /api/users/:id - Fetch user by ID
POST /api/users - Create new user
PUT /api/users/:id - Update user
DELETE /api/users/:id - Delete userDependencies
- express (^5.2.1) - Web framework
- mysql2 (^3.22.5) - MySQL database driver
- bcrypt (^6.0.0) - Password hashing
- jsonwebtoken (^9.0.3) - JWT authentication
- dotenv (^17.4.2) - Environment variable management
- cookie-parser (^1.4.7) - Cookie parsing middleware
Authentication
The generated authMiddleware.js provides JWT-based authentication. Use it to protect routes:
const authMiddleware = require('./API/middleware/authMiddleware');
app.get('/api/protected-route', authMiddleware, (req, res) => {
// Protected route - user is in req.user
res.json({ user: req.user });
});Generated Files Details
Models (API/models/*.js)
- Database query functions with callbacks
- Password hashing integration
- Email lookup and other utility queries
Controllers (API/controllers/*.js)
- Request handlers
- Business logic
- Error handling
Routes (API/routes/*.js)
- Express router configuration
- HTTP method mapping to controllers
Config (API/config/db.js)
- MySQL connection pool setup
- Environment variable integration
Middleware (API/middleware/authMiddleware.js)
- JWT token verification
- User authentication
Server (server.js)
- Express app initialization
- Route mounting
- Server startup on configured PORT
Tips
- Use
.request.restfor testing API endpoints (requires REST Client extension in VS Code) - Update
resource.jsonwith all tables before installing - Review generated code - it's a starting template, customize as needed
- Add business logic to controllers after generation
- Modify models to match your exact database schema
License
MIT
DELETE /api/users/:id- Delete user
Notes
- The postinstall script runs automatically when the package is installed
- It only creates files if they don't already exist
- If
resource.jsonis not found, scaffolding is skipped silently - If
resource.jsondoesn't contain a"users"resource, scaffolding is skipped
Publishing
To publish this package to npm:
cd putitdigital-api-express
npm publish --otp=<your-2fa-code>License
MIT
resource.json example
{ "tables": { "user": { "columns": [ "name", "email", "password" ], "action":[ "create", "read", "findByEmail", "update", "delete" ] },"product": { "columns": [ "name", "description", "price" ], "action":[ "create", "read", "findByEmail", "update", "delete" ] } } }
