/teknologia-backend

the backend for teknologia finall round project

Primary LanguageJavaScript

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.