This is the backend server for the Smart Diary App, a full-stack web application that allows users to write diary entries and receive personalized auto-responses based on the sentiment of their entries.
- Node.js (Express) - Backend server
- MongoDB - Database (via Mongoose)
- JWT - Authentication
- Axios - External API request (Google NLP, OpenAI GPT-3)
- dotenv - Environment variables
- bcrypt - Password hashing
- cors - Cross-origin resource sharing
- rate-limiter - Rate limiting for API requests (100 requests per 15 minutes)
- express-validator - Request validation
- User collection: username, email, password (hashed with bcrypt), createdAt (validation at database level + express-validator)
- DiaryEntry collection: user_id, text_entry, auto_response, sentiment_score, sentiment_magnitude, createdAt, updatedAt
- Register/Login routes: Hash password with bcrypt. Use JWT tokens to protect routes.
- Middleware: Check token validity and user access rights.
- Create: Post new diary entry
- Read: Get all diary entries for a user
- Update: Modify existing diary entry
- Delete: Remove diary entry
- Sentiment Analysis: Pass the diary entry to Google Cloud Natural Language API.
- Response Data: Extract sentiment score and magnitude from the API response.
- Personalized auto-response: Based on the sentiment analysis result, generate a thoughtful and empathetic response using the OpenAI GPT-3 API.
- The response is generated by providing a prompt that includes the text entry and sentiment information.
- Store sensitive information like API keys, database connection string in a .env file.
- Trend Analysis: Aggregate and return sentiment scores over time (for future use on the frontend visualization like graphs and word clouds).
-
Authentication Routes (/api/auth)
- POST /register: Create a new user, store hashed password.
- POST /login: Validate user credentials, return JWT token.
-
Diary Entry Routes (/api/entries)
- GET /: Fetch all user diary entries (protected route).
- POST /: Create a new diary entry (protected route, triggers sentiment analysis & GPT-3 response).
- PUT /:id: Update an existing entry by ID (protected route).
- DELETE /:id: Delete an entry by ID (protected route).
-
Sentiment Analysis (via axios)
axios.post('https://language.googleapis.com/v1/documents:analyze')
- Google Cloud Natural Language API.
-
Auto-Response Generation (via OpenAI API - downloaded as a package)
openai.chatCompletion.create()
- OpenAI GPT-3 API.
-
Clone the repository:
git clone https://github.com/SahandNamvar/smart-diary-app-backend.git
-
Navigate to the backend directory:
cd smart-diary-app-backend
-
Install dependencies:
npm install
- Ensure you have Node.js and npm installed on your machine. You can check if they are installed by running:
node -v
andnpm -v
. - Next, install the required dependencies by running
npm install
.
- Create a .env file in the root directory and add the following environment variables: (contact me for the API keys)
MONGO_URI=<your-mongodb-connection-string>
JWT_SECRET=<your-jwt-secret-key>
GOOGLE_API_KEY=<your-google-cloud-api-key>
OPENAI_API_KEY=<your-openai-api-key>
- Run the backend server:
npm start
ornpm run dev
(with nodemon)
- You should see a message saying:
Server running on port 5000 🚀
andConnected to MongoDB 🟢
- Test the API endpoints using Postman or any other API testing tool.
- Once the server is running, you can interact with the backend APIs via the following routes:
- Authentication:
POST /api/auth/register
: Register a new user. - Authentication:
POST /api/auth/login
: Login and get a JWT token. - Diary Entries:
GET /api/entries
: Fetch all diary entries for the logged-in user. - Diary Entries:
POST /api/entries
: Create a new diary entry (automatically triggers sentiment analysis and auto-response generation). - Diary Entries:
PUT /api/entries/:id
: Update an existing diary entry by ID. - Diary Entries:
DELETE /api/entries/:id
: Delete a diary entry by ID.
- Authentication:
Make sure to include the JWT token in the Authorization header for protected routes.