A modular, production-ready Express.js API using TypeScript, MySQL, and Joi for authentication workflows (login, register, forgot password, OTP, etc.).
- Modular folder structure (per-feature modules)
- TypeScript for type safety
- MySQL database integration (via
mysql2) - Secure password hashing with bcryptjs
- OTP generation and validation
- Request validation with Joi middleware
- Common response and logger utilities
template-js/
├── src/
│ ├── app.ts
│ ├── server.ts
│ ├── config/
│ │ ├── db.ts
│ │ └── env.ts
│ ├── common/
│ │ ├── logger.ts
│ │ ├── response.ts
│ │ └── validate.ts
│ ├── modules/
│ │ ├── auth/
│ │ │ ├── auth.controller.ts
│ │ │ ├── auth.model.ts
│ │ │ ├── auth.routes.ts
│ │ │ └── auth.service.ts
│ │ └── user/ ...
│ └── utils/ ...
├── package.json
├── tsconfig.json
└── README.md
- Install dependencies:
npm install
- Configure environment variables:
- Create a
.envfile or set these variables:DB_HOST(default:localhost)DB_USER(default:root)DB_PASS(default: empty)DB_NAME(default:app_db)PORT(default:3000)
- Create a
- Start MySQL server and ensure the database exists (the
userstable is auto-created). - Run the app:
- Development:
npm run dev - Production:
npm run build && npm start
- Development:
All endpoints are prefixed with /api/auth.
- POST
/api/auth/login- Body:
{ "username": string, "password": string }
- Body:
- POST
/api/auth/register- Body:
{ "username": string, "password": string, "email": string }
- Body:
- POST
/api/auth/forgot- Body:
{ "email": string }
- Body:
- POST
/api/auth/send-otp- Body:
{ "email": string }
- Body:
- POST
/api/auth/validate-otp- Body:
{ "email": string, "otp": string }
- Body:
All responses use a common format:
{
status: 'success' | 'error',
message: string,
data?: any,
code?: number
}
All request bodies are validated with Joi. Invalid requests return a 400 error with details.
- Add new modules in
src/modules/(copy theauthpattern) - Add new routes, controllers, and services as needed
MIT