Code Monkey home page Code Monkey logo

builderlab-backend's Introduction

πŸ“ 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 [[email protected]].

builderlab-backend's People

Contributors

deshan555 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.