A modularized Express.js API for managing checklists and templates with MongoDB. This project includes error handling, logging, and performance monitoring.
- Modularized route structure for better maintainability.
- Comprehensive error handling.
- Performance monitoring middleware.
- Logging using Winston.
project
β .env
β package.json
β server.js
ββββconfig
β db.js
β logger.js
ββββroutes
β data.js
β checklists.js
ββββmiddlewares
errorHandler.js
performanceMonitor.js
- Node.js
- npm (or yarn)
- MongoDB Atlas account (or a local MongoDB instance)
-
Clone the repository:
git clone https://github.com/yourusername/checklist-api.git cd checklist-api -
Install dependencies:
npm install
-
Create a
.envfile in the root directory and add your MongoDB connection details:USERNAMED=yourUsername PASSWORD=yourPassword CLUSTER=yourCluster OPTIONS=yourOptions PORT=3001 -
Start the server:
npm start
- GET
/data- Retrieve all templates - POST
/data- Create a new template - PUT
/data/:id- Update a template by ID - DELETE
/data/:id- Delete a template by ID
- GET
/checklists- Retrieve all checklists - GET
/checklists/:id- Retrieve a checklist by ID - POST
/checklists- Create a new checklist - PUT
/checklists/:id- Update a checklist by ID - DELETE
/checklists/:id- Delete a checklist by ID
- Database Configuration: Handled in
config/db.js. - Error Handling Middleware: Implemented in
middlewares/errorHandler.js. - Performance Monitoring Middleware: Implemented in
middlewares/performanceMonitor.js. - Logging Configuration: Set up in
config/logger.js.
Handles any errors that occur during the request-response cycle.
// middlewares/errorHandler.js function errorHandler(err, req, res, next) { console.error(err.stack); res.status(500).json({ error: 'Something went wrong!' }); } module.exports = errorHandler;
Logs the duration of each request.
// middlewares/performanceMonitor.js const performanceMonitor = (req, res, next) => { const startHrTime = process.hrtime(); res.on('finish', () => { const elapsedHrTime = process.hrtime(startHrTime); const elapsedTimeInMs = elapsedHrTime[0] * 1000 + elapsedHrTime[1] / 1e6; console.log(`${req.method} ${req.url} [${res.statusCode}] - ${elapsedTimeInMs}ms`); }); next(); }; module.exports = performanceMonitor;
Configured using Winston for comprehensive logging.
// config/logger.js const winston = require('winston'); const logger = winston.createLogger({ level: 'info', format: winston.format.combine( winston.format.timestamp(), winston.format.json() ), transports: [ new winston.transports.File({ filename: 'error.log', level: 'error' }), new winston.transports.File({ filename: 'combined.log' }), ], }); if (process.env.NODE_ENV !== 'production') { logger.add(new winston.transports.Console({ format: winston.format.simple(), })); } module.exports = logger;
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.
For any inquiries, please reach out at [djayashanka750@gmail.com].