Git Product home page Git Product logo

teknologia-backend's Introduction

Express Server Documentation

Overview

This document outlines the functionality and usage of an Express.js server implemented with various middleware and routes for handling data and file uploads. The server includes endpoints for retrieving, uploading, and deleting data, as well as connection with a database.

Dependencies

  • Express: A web application framework for Node.js.
  • bcrypt: A library for hashing passwords.
  • mongoose: An Object Data Modeling (ODM) library for MongoDB and Node.js.
  • dotenv: A zero-dependency module for loading environment variables from a .env file into process.env.
  • multer: A middleware for handling multipart/form-data, primarily used for file uploads.
  • cors: A middleware for enabling Cross-Origin Resource Sharing (CORS) in Express.js applications.

Configuration

  1. Express Setup: The server is initialized using Express.
  2. Environment Variables: Environment variables are loaded from a .env file using dotenv.
  3. Database Connection: Connection to a MongoDB database is established using the connection object.
  4. Multer Storage Configuration: Multer middleware is configured to store uploaded files in memory.
  5. CORS Setup: Cross-Origin Resource Sharing is enabled using the cors middleware.
  6. Server Port: The server listens on port 8080.

Routes

  1. GET /data:
    • Retrieves paginated data from the database.
    • Parameters:
      • page: Optional query parameter specifying the page number (default is 1).
    • Returns:
      • JSON response containing retrieved data, current page, total pages, and total count.
    • Error Handling:
      • Returns a 500 status with an error message if data retrieval fails.
  2. POST /upload:
    • Handles file uploads along with user data.
    • Parameters:
      • name: User name.
      • password: User password.
      • resume: File upload (expects a file named "resume").
    • Returns:
      • JSON response containing saved data if successful.
    • Error Handling:
      • Returns a 400 status if a duplicate resume is detected.
      • Returns a 500 status with an error message if upload fails.
  3. DELETE /data/:id:
    • Deletes data by ID from the database.
    • Parameters:
      • id: ID of the data to be deleted.
    • Returns:
      • JSON response containing deleted data if successful.
    • Error Handling:
      • Returns a 404 status if data with the given ID is not found.
      • Returns a 500 status with an error message if deletion fails.

Constants

  • ITEMS_PER_PAGE: Defines the number of items to display per page in paginated results.

Middleware

  • express.json(): Parses incoming request bodies with JSON payloads.
  • upload.single("resume"): Handles single file uploads with the field name "resume".

Error Handling

  • Errors are logged to the console and appropriate HTTP status codes and error messages are returned in case of failures.

Server Initialization

  • The server listens on port 8080.
  • Connection to the database is established asynchronously, with console logs indicating successful connection or any errors encountered.

teknologia-backend's People

Contributors

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