- Overview
- Technologies Used
- Project Structure
- Installation
- Configuration
- Running the Application
- Endpoints
- Error Handling
- Models
This repository contains a Node.js API built with Express.js, providing user authentication using JWT (JSON Web Tokens), CRUD (Create, Read, Update, Delete) operations for products, and file upload/download functionalities.
- Node.js: A JavaScript runtime for building the server-side application.
- Express.js: A fast, unopinionated, and minimalist web framework for Node.js.
- MongoDB: A NoSQL database used to store user and product data.
- jsonwebtoken: A library to generate and verify JSON Web Tokens for user authentication.
- bcrypt: A library to hash and compare passwords securely.
- multer: A middleware for handling
multipart/form-data
, used for file upload. - mongoose: An ODM (Object Data Modeling) library for MongoDB and Node.js.
.
├── index.js
├── routes
│ ├── auth-routes.js
│ ├── productRoutes.js
│ └── file-routes.js
├── controllers
│ ├── auth-controller.js
│ ├── product-controller.js
│ └── file-controller.js
├── middlewares
│ └── auth-middleware.js
├── models
│ ├── user-model.js
│ ├── user-product-model.js
│ ├── file-model.js
│ └── token-blacklist-model.js
├── constants
│ └── constants.js
└── utils
├── error-handler.js
└── file-storage-config.js
- Clone the repository:
git clone https://github.com/sabari570/Backend-Task-2-JWT-authenticated-APIs-for-user-product-and-file-manipulation-.git
- Navigate to the project directory:
cd nodejs-express-jwt-crud
- Install dependencies:
npm install
Create a `.env` file in the root directory and add the following:
JWT_ACCESS_TOKEN_SECRET_KEY=your_access_token_secret_key
JWT_REFRESH_TOKEN_SECRET_KEY=your_refresh_token_secret_key
PORT=port_number_to_run_the_server
npm run dev
POST /api/auth/register
: Register a new userPOST /api/auth/login
: Authenticate and login a userPOST /api/auth/regenerate-token
: Refresh the JWT tokenPOST /api/auth/logout
: Logout a user
POST /api/products/create-product
: Create a new productGET /api/products/fetch-products
: Fetch all products for a userGET /api/products/fetch-product/:id
: Fetch a single product by IDPUT /api/products/update-product/:id
: Update a product by IDDELETE /api/products/delete-product/:id
: Delete a product by ID
POST /api/file/upload-file
: Upload a fileGET /api/file/download-file/:fileId
: Download a file by ID
Errors are handled using a custom error handler utility (error-handler.js
) which formats the error messages.
The mongoose model for the User schema.
The mongoose model for the UserProducts schema.
The mongoose model for storing file details related to the user.
The mongoose model for storing the blacklisted tokens.