Skip to content

πŸ“ A modularized Express.js API for managing checklists and templates with MongoDB. This project includes error handling, logging, and performance monitoring.

License

Notifications You must be signed in to change notification settings

Deshan555/BuilderLab-Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

30 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“ Checklist API

A modularized Express.js API for managing checklists and templates with MongoDB. This project includes error handling, logging, and performance monitoring.

🌟 Features

  • Modularized route structure for better maintainability.
  • Comprehensive error handling.
  • Performance monitoring middleware.
  • Logging using Winston.

πŸ“ Project Structure

project
β”‚   .env
β”‚   package.json
β”‚   server.js
└───config
β”‚       db.js
β”‚       logger.js
└───routes
β”‚       data.js
β”‚       checklists.js
└───middlewares
        errorHandler.js
        performanceMonitor.js

πŸš€ Getting Started

Prerequisites

  • Node.js
  • npm (or yarn)
  • MongoDB Atlas account (or a local MongoDB instance)

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/checklist-api.git
    cd checklist-api
  2. Install dependencies:

    npm install
  3. Create a .env file in the root directory and add your MongoDB connection details:

    USERNAMED=yourUsername
    PASSWORD=yourPassword
    CLUSTER=yourCluster
    OPTIONS=yourOptions
    PORT=3001
    
  4. Start the server:

    npm start

πŸ”Œ API Endpoints

Templates

  • 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

Checklists

  • 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

πŸ› οΈ Configuration

  • 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.

πŸ“œ Middleware

Error Handler

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;

Performance Monitor

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;

πŸ“ Logging

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;

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“œ License

This project is licensed under the MIT License.

πŸ’¬ Contact

For any inquiries, please reach out at [djayashanka750@gmail.com].

About

πŸ“ A modularized Express.js API for managing checklists and templates with MongoDB. This project includes error handling, logging, and performance monitoring.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published