Skip to content

Backend application for managing diary entries with features like sentiment analysis and AI-generated responses. Utilizes technologies such as MongoDB, JWT authentication, and integrates with Google NLP and OpenAI APIs.

Notifications You must be signed in to change notification settings

SahandNamvar/smart-diary-app-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Smart Diary App - Backend

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.


1. Backend Project Setup (Dependencies)

  • 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

2. MongoDB Collection Schema

  • 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

3. JWT Authentication and Authorization

  • Register/Login routes: Hash password with bcrypt. Use JWT tokens to protect routes.
  • Middleware: Check token validity and user access rights.

4. CRUD Operations for Diary Entries

  • Create: Post new diary entry
  • Read: Get all diary entries for a user
  • Update: Modify existing diary entry
  • Delete: Remove diary entry

5. Google NLP API Integration

  • Sentiment Analysis: Pass the diary entry to Google Cloud Natural Language API.
  • Response Data: Extract sentiment score and magnitude from the API response.

6. OpenAI GPT-3 API Integration (gpt-3.5-turbo engine)

  • 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.

7. Environment Variables

  • Store sensitive information like API keys, database connection string in a .env file.

8. Data Visualization (Future Work)

  • Trend Analysis: Aggregate and return sentiment scores over time (for future use on the frontend visualization like graphs and word clouds).

Backend API Endpoints

  1. Authentication Routes (/api/auth)

    • POST /register: Create a new user, store hashed password.
    • POST /login: Validate user credentials, return JWT token.
  2. 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).
  3. Sentiment Analysis (via axios)

    • axios.post('https://language.googleapis.com/v1/documents:analyze') - Google Cloud Natural Language API.
  4. Auto-Response Generation (via OpenAI API - downloaded as a package)

    • openai.chatCompletion.create() - OpenAI GPT-3 API.

How to Download and Run the Smart Diary App Backend

Follow these steps to set up the backend on your local machine:

  1. Clone the repository: git clone https://github.com/SahandNamvar/smart-diary-app-backend.git

  2. Navigate to the backend directory: cd smart-diary-app-backend

  3. 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 and npm -v.
  • Next, install the required dependencies by running npm install.
  1. 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>
  1. Run the backend server: npm start or npm run dev (with nodemon)
  • You should see a message saying: Server running on port 5000 🚀 and Connected to MongoDB 🟢
  1. 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.

Make sure to include the JWT token in the Authorization header for protected routes.


About

Backend application for managing diary entries with features like sentiment analysis and AI-generated responses. Utilizes technologies such as MongoDB, JWT authentication, and integrates with Google NLP and OpenAI APIs.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published