Skip to content

Miro-wq/nodejs-rest-api

 
 

Repository files navigation

Node.js REST API with Docker and Email Verification

This is a complete Node.js REST API application that includes features such as user authentication, email verification, and Docker integration. The application is built with Express.js, MongoDB, and SendGrid for email services.


Features

  • User signup and login with email/password authentication.
  • Email verification using SendGrid.
  • Protected routes with JWT-based authentication.
  • Contact management (CRUD operations) for authenticated users.
  • Dockerized for portability and ease of deployment.

Getting Started

Follow these instructions to get the application running on your local machine or in a Docker container.

Prerequisites

Make sure you have the following installed:


Installation

  1. Clone this repository:
git clone https://github.com/<your-username>/<your-repo>.git
cd <your-repo>
  1. Install dependencies:
npm install
  1. Create a .env file in the root directory and add the following environment variables:
DB_USERNAME=your_username
DB_PASSWORD=your_password
DB_HOST=mongodb+srv://username:password@cluster0.oc5wu.mongodb.net/db-contacts?retryWrites=true&w=majority
SECRET_KEY=your_secret_key
SENDGRID_API_KEY=your_sendgrid_api_key
  1. Start the application:
  npm start

The server will run on http://localhost:3000.


Running with Docker

  1. Build the Docker image
docker build -t nodejs-app .
  1. Run the Docker container You can pass environment variables directly:
docker run -p 3000:3000 \
  -e DB_USERNAME=your_username \
  -e DB_PASSWORD=your_password \
  -e DB_HOST=mongodb+srv://username:password@cluster0.oc5wu.mongodb.net/db-contacts?retryWrites=true&w=majority \
  -e SECRET_KEY=your_secret_key \
  -e SENDGRID_API_KEY=your_sendgrid_api_key \
  nodejs-app

Or use a .env file:

docker run -p 3000:3000 --env-file .env nodejs-app

API Documentation

Authentication

Signup

  • POST /api/users/signup
  • Request Body:
{
  "email": "example@example.com",
  "password": "examplepassword"
}
  • Response:
{
  "user": {
    "email": "example@example.com",
    "subscription": "starter",
    "avatarURL": "https://gravatar.com/avatar/..."
  }
}

Login

  • POST /api/users/login
  • Request Body:
{
  "email": "example@example.com",
  "password": "examplepassword"
}
  • Response:
{
  "token": "your-jwt-token",
  "user": {
    "email": "example@example.com",
    "subscription": "starter"
  }
}

Email Verification

  • GET /api/users/verify/:verificationToken
  • Verifies the user's email.

Resend Verification Email

  • POST /api/users/verify
  • Request Body:
{
  "email": "example@example.com"
}

Contacts

Get All Contacts

  • GET /api/contacts Create a Contact
  • POST /api/contacts
  • Request Body:
{
  "name": "John Doe",
  "email": "john@example.com",
  "phone": "1234567890"
}

Update Contact

  • PUT /api/contacts/:contactId

Delete Contact

  • DELETE /api/contacts/:contactId

Folder Structure

.
├── Dockerfile
├── .dockerignore
├── .env.example
├── README.md
├── package.json
├── app.js
├── server.js
├── models/
│   └── user.js
│   └── contact.js
├── routes/
│   └── api/
│       └── users.js
│       └── contacts.js
├── helpers/
│   └── sendEmail.js

About

This project is a RESTful API developed using Node.js, Express.js, and MongoDB, designed to efficiently manage a contact database. It provides a robust and scalable set of HTTP endpoints, enabling users to perform CRUD (Create, Read, Update, Delete) operations on contact records.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • JavaScript 100.0%