Portfolio API Backend
This is a simple and efficient backend server built with Node.js and Express. Its primary purpose is to handle the contact form submissions from the portfolio frontend, sending emails via SMTP using Nodemailer.
✨ Features
- Email Service: Securely sends emails from the contact form.
- Input Validation: Ensures required fields (
name,email,message) are present and the email format is valid. - CORS Enabled: Configured with CORS to only accept requests from the frontend application.
- Specific Error Handling: Returns structured error keys for different SMTP and server issues, allowing the frontend to display translated, user-friendly messages.
- Health Check: Includes a
/api/healthendpoint to easily verify if the server is running.
🛠️ Tech Stack
- Runtime: Node.js
- Framework: Express.js
- Email: Nodemailer
- Environment Variables: Dotenv
- Cross-Origin Requests: CORS
🚀 Getting Started
Prerequisites
- Node.js (v18 or later)
- npm
Installation & Setup
-
Navigate to the backend directory:
cd backend -
Install dependencies:
npm install -
Create an environment file: Create a file named
.envin thebackenddirectory and populate it with your credentials. Do not commit this file to version control..env.example
# The port for the backend server BACKEND_PORT=3001 # Your frontend URL for CORS FRONTEND_URL=http://localhost:3000 # Nodemailer SMTP Configuration SMTP_HOST=smtp.example.com SMTP_PORT=587 SMTP_SECURE=false SMTP_USER=your-email@example.com SMTP_PASS=your-email-password YOUR_RECEIVING_EMAIL=your-personal-email@example.com -
Run the server:
npm startThe server will start on the port defined in your
.envfile (e.g.,3001).
📝 API Endpoints
Health Check
- GET
/api/health- Description: Checks the server status.
- Success Response (200):
{ "status": "UP", "message": "Backend is running" }
Send Email
- POST
/api/email/send- Description: Processes and sends a contact form submission.
- Request Body:
{ "name": "string", "email": "string", "message": "string" } - Success Response (200):
{ "message": "Message sent successfully!" } - Error Responses (4xx/5xx): Returns a JSON object with an
errorKeyfor the frontend to translate.{ "errorKey": "smtp_auth_failed" }