-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
63 lines (52 loc) · 1.89 KB
/
server.js
File metadata and controls
63 lines (52 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// server.js
// Import dependencies
require('dotenv').config();
console.log("Mongo URI:", process.env.MONGO_URI); // Add this line to check the value
const express = require('express');
const mongoose = require('mongoose');
const axios = require('axios');
const cors = require('cors');
// Create an Express application
const app = express();
// Middleware setup
app.use(cors());
app.use(express.json());
// Port for the server
const PORT = process.env.PORT || 5000;
// MongoDB connection
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB connected'))
.catch((err) => console.error('MongoDB connection error:', err));
// Define the routes after defining `app`
// This is an example route; make sure you have the Chat model and other details set up
app.post('/api/parse', async (req, res) => {
const { link } = req.body;
try {
const response = await axios.get(link);
const parsedData = {
human: "Sample Human Input",
ai: "Sample AI Response"
};
// Use your MongoDB Chat model here (this is a placeholder example)
// const chat = new Chat({ url: link, human: parsedData.human, ai: parsedData.ai });
// await chat.save();
res.status(200).json({ message: 'Chat successfully saved!' });
} catch (error) {
res.status(500).json({ message: 'Failed to parse link', error: error.message });
}
});
// Example route to get all chats (adjust this according to your model)
app.get('/api/chats', async (req, res) => {
try {
// Replace with your MongoDB query
// const chats = await Chat.find();
// res.json(chats);
res.json([]); // Temporary placeholder response
} catch (err) {
res.status(500).json({ message: 'Error retrieving chats', error: err.message });
}
});
// Start the server
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});