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.
- Express: A web application framework for Node.js.
- bcrypt: A library for hashing passwords.
- Sequelize: A promise-based Node.js ORM for MySQL, PostgreSQL, MariaDB, SQLite, and Microsoft SQL Server.
- dotenv: A zero-dependency module for loading environment variables from a
.env
file intoprocess.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.
- Express Setup: The server is initialized using Express.
- Environment Variables: Environment variables are loaded from a
.env
file usingdotenv
. - Database Connection: Connection to a MySQL database is established using Sequelize.
- Multer Storage Configuration: Multer middleware is configured to store uploaded files in memory.
- CORS Setup: Cross-Origin Resource Sharing is enabled using the
cors
middleware. - Server Port: The server listens on port 8080.
- 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.
- 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.
- 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.
- ITEMS_PER_PAGE: Defines the number of items to display per page in paginated results.
- express.json(): Parses incoming request bodies with JSON payloads.
- upload.single("resume"): Handles single file uploads with the field name "resume".
- Errors are logged to the console and appropriate HTTP status codes and error messages are returned in case of failures.
- The server listens on port 8080.
- Connection to the database is established asynchronously, with console logs indicating successful connection or any errors encountered.