Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Backend Foundation

Backend Foundation is my personal backend learning repository where I document and practice core backend development concepts by building real-world projects using Node.js, Express.js, and MongoDB. This repository tracks my journey of learning server-side development, authentication, APIs, database integration, and scalable backend architecture.



Terminal-Based Project Setup

Follow these terminal commands to set up the initial backend project structure from scratch.

Step 1: Create the project folder and move into it

mkdir backend-foundation && \
cd backend-foundation

Step 2: Initialize the Node.js project and generate package.json

npm init

Step 3: Install Prettier as a development dependency

npm install --save-dev --save-exact prettier

Step 4: Create the complete project folder structure in a single command

mkdir public src && \
mkdir public/images && \
cd src && \
mkdir controllers models routes middlewares utils db validators && \
touch app.js index.js && \
touch controllers/.gitkeep models/.gitkeep routes/.gitkeep middlewares/.gitkeep utils/.gitkeep db/.gitkeep validators/.gitkeep && \
cd .. && \
touch public/images/.gitkeep .env .gitignore .prettierrc .prettierignore

Step 5: Configure .gitignore

node_modules/
.env
.vscode/
.DS_Store
dist/

Step 6: Configure .prettierrc

{
  "tabWidth": 2,
  "useTabs": false,
  "semi": true,
  "singleQuote": false,
  "trailingComma": "all",
  "bracketSpacing": true,
  "arrowParens": "always"
}

Step 7: Configure .prettierignore

node_modules
dist
.env
package-lock.json

Project Structure

Backend Structure



Installing Required Dependencies

Install dotenv to manage environment variables from a .env file.

npm i dotenv

Install nodemon to automatically restart the server when file changes are detected.

npm i nodemon

Installs Express framework for building backend APIs and handling routes.

npm i express

Enables CORS for handling cross-origin requests.

npm i cors

Installs Mongoose for interacting with MongoDB using schemas and models.

npm i mongoose

Installs bcrypt for hashing and securing user passwords.

npm i bcrypt

Installs JSON Web Token for authentication and secure token generation.

npm i jsonwebtoken

Provides cryptographic functionality for hashing, encryption, and secure token generation.

npm i crypto

Installs Nodemailer for sending emails from the application.

npm i nodemailer

Installs Mailgen for generating beautiful email templates.

npm i mailgen

Installs express-validator for validating and sanitizing incoming request data.

npm i express-validator

Installs cookie-parser for parsing cookies from incoming client requests.

npm i cookie-parser

Installs Resend for handling and sending transactional emails.

npm install resend


API Documentation

Content Type

All request bodies must use:

Content-Type: application/json

POST auth/register

Request Body:

{
  "username": "testuser",
  "email": "testuser@example.com",
  "password": "TestPass123"
}

POST auth/verify-email

Verify a user's email address using the verification token and email received through the URL parameters

Request Body:

{
  "verificationToken:": "EMAIL_VERIFICATION_TOKEN"
}

POST auth/resend-email-verification

Send a new email verification link by using the expired token and email received through the URL parameters when the previous verification token has expired. Request Body:

{
  "expiredToken": "EXPIRED_TOKEN",
  "email": "testuser@example.com"
}

POST /auth/login

Request Body:

{
  "username": "testuser",
  "email": "testuser@example.com",
  "password": "TestPass123"
}

POST /auth/forgot-password

Request Body:

{
  "email": "testuser@example.com"
}

POST /auth/reset-password

Reset the user's password using the reset token received via email.
Request Body:

{
  "token": "RESET_TOKEN_FROM_EMAIL",
  "newPassword": "NewPass123"
}

POST /auth/change-password

Protected Route:

Authorization: Bearer <access_token>

Request Body:

{
  "oldPassword": "OldPass123",
  "newPassword": "NewPass123"
}

POST /auth/current-user

Retrieve the profile details of the currently authenticated user using the access token.
Protected Route:

Authorization: Bearer <access_token>

OR

Cookie: accessToken=<httpOnly-cookie>

POST /healthcheck

Check whether the server is running and responding correctly.


POST /auth/refresh-token

Request Body:

{
  "refreshToken": "your_refresh_token"
}

OR

Cookie: accessToken=<httpOnly-cookie>

POST /auth/logout

Protected Route:

Authorization: Bearer <access_token>

OR

Cookie: accessToken=<httpOnly-cookie>

About

A backend development guide created while learning core concepts step by step, updated as I continue learning.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages