Proyek ini menyediakan API backend untuk sistem jawaban pertanyaan yang ditenagai oleh model bahasa generatif Google AI. Pengguna dapat mengirimkan pertanyaan, menerima jawaban yang dihasilkan oleh model AI, dan secara opsional menyimpan pertanyaan dan jawaban mereka untuk referensi di masa mendatang.
Klon Repositori ini:
git clone [https://github.com/Faizgeeky/faiz-mohammad-answersai-backend.git](https://github.com/Faizgeeky/faiz-mohammad-answersai-backend.git)
Arahkan ke Direktori Proyek:
cd faiz-mohammad-answersai-backend
3- Pasang dependensi:
npm i
Configuration
# # 1. Environment Variables:
# Create a file named .env in your project's root directory. This file should contain the following environment variables (replace with your actual values):
Access_token_secret = your_secret_key mongo_uri = mongoDb: // your_mongo_connection_string gemini_api_key = your_google_cloud_api_key
# Important:
ACCESS_TOKEN_SECRET: A secret key used for signing JWT tokens for user authentication. Choose a strong and unique key.
MONGO_URI: The connection string for your MongoDB database.
GEMINI_API_KEY: Your Google Cloud API key for accessing the Generative AI service.
Note: You can exclude the .env file from version control using tools like .gitignore.
## 2. MongoDB Database:
Set up a MongoDB database instance and configure the MONGO_URI environment variable accordingly.
Running the Application
Start the development server:
NPM Mulai # atau Benang Mulai
Use code with caution.
content_copy
This will start the Node.js server and listen for incoming requests on the default port (usually 8000).
(Optional) Start a live-reloading server for development:
NPM Run Server # atau Server Benang
Use code with caution.
content_copy
This will use Nodemon to automatically restart the server whenever you make changes to your code.
API Usage
The API endpoints are documented below. You can use tools like Postman or curl to send requests to the server.
1. User Authentication:
# Endpoint: /api/users/ (POST)
Json {"nama pengguna": "your_username", "kata sandi": "your_password"}
Badan Permintaan:
{
"username": "your_username",
"password": "your_password"
}
Tanggapan:
Json {"token": "your_jwt_token"}
Setelah login yang berhasil, server mengembalikan token JWT yang harus Anda sertakan dalam permintaan berikutnya yang memerlukan otentikasi.
Titik Akhir: /API /Pertanyaan (Posting)
Minta header:
Otorisasi: pembawa <your_jwt_token> badan permintaan:
Json {"pertanyaan": "your_question"} Gunakan kode dengan hati -hati. Content_copy Response:
Json {"_id": "question_id", "user_id": "user_id", "konten": "your_question", "Jawab": "ai_generated_answer", "createdat": "timestamp"} menggunakan kode dengan hati -hati. content_copy 3. ambil pertanyaan:
Titik akhir: /api/questions (Dapatkan)
Tanggapan:
[
{ ...question1 object ... },
{ ...question2 object ... },
...
] Titik akhir: /api/questions/user/<user_id> (dapatkan)
Tanggapan: