Skip to content

thejaved/cloth-shop-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Absolutely! Here's a concise documentation outline for the backend setup we've created:


E-Commerce Backend Documentation

Introduction

This documentation covers the setup and usage of the backend for an e-commerce application built using Node.js, Express, TypeScript, and MongoDB.

Setup

Prerequisites

  • Node.js (latest LTS version recommended)
  • MongoDB (installed and running locally or accessible remotely)
  • npm (Node Package Manager)

Installation

  1. Clone the repository from GitHub:

    git clone <repository-url>
    cd <project-directory>
  2. Install dependencies:

    npm install
  3. Set up environment variables:

    Create a .env file in the root directory with the following variables:

    PORT=5000
    MONGO_URI=mongodb://localhost:27017/ecommerce
    JWT_SECRET=your_jwt_secret
    

Folder Structure

├── src/
│   ├── config/
│   │   └── db.ts             # Database configuration (MongoDB connection)
│   ├── controllers/
│   │   ├── authController.ts # Controllers for authentication (register, login)
│   ├── middleware/
│   │   └── auth.ts           # Middleware for JWT authentication
│   ├── models/
│   │   └── User.ts           # Mongoose schema for User model (with password hashing)
│   ├── routes/
│   │   └── authRoutes.ts     # Express routes for authentication endpoints
│   ├── app.ts                # Main Express application setup
│   └── server.ts             # Server initialization
├── .env                      # Environment variables configuration
├── package.json              # Project dependencies and scripts
└── tsconfig.json             # TypeScript configuration

Usage

Starting the Server

To start the server, run:

npm start

The server will run on localhost:5000 by default (or the port specified in your .env file).

API Endpoints

Authentication

  • Register User:

    POST /api/auth/register

    Request Body:

    {
      "name": "John Doe",
      "email": "[email protected]",
      "password": "password123"
    }
  • Login User:

    POST /api/auth/login

    Request Body:

    {
      "email": "[email protected]",
      "password": "password123"
    }

    Response:

    {
      "token": "your_jwt_token"
    }

Authentication Middleware

Protected routes can be created by using the auth middleware. Example:

import express from "express";
import { auth } from "../middleware/auth";

const router = express.Router();

router.get("/protected-route", auth, (req, res) => {
  // Access authenticated user via req.user
  res.json({ msg: "Authenticated route", user: req.user });
});

export default router;

Error Handling

  • Validation errors and server errors are handled centrally within controllers.
  • Error messages are returned as JSON responses with appropriate HTTP status codes.

Dependencies

  • Express: Web framework for Node.js.
  • Mongoose: MongoDB object modeling tool.
  • bcrypt: Password hashing library.
  • jsonwebtoken: JWT implementation for Node.js.
  • dotenv: Environment variable loader.

Conclusion

This documentation provides a comprehensive guide to setting up and using the backend for an e-commerce application. For more details on specific endpoints or configurations, refer to the respective source files in the src directory.


This outline covers the essential aspects of your backend setup, including setup instructions, folder structure, usage examples, API endpoints, authentication middleware, error handling, dependencies, and a conclusion summarizing the documentation. Adjust it further based on specific details or additional functionalities you might add to your application.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published