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.
- 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.
Follow these instructions to get the application running on your local machine or in a Docker container.
Make sure you have the following installed:
- Clone this repository:
git clone https://github.com/<your-username>/<your-repo>.git
cd <your-repo>
- Install dependencies:
npm install
- 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
- Start the application:
npm start
The server will run on http://localhost:3000.
- Build the Docker image
docker build -t nodejs-app .
- 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
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
.
├── 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