Files
CatGPT/server.js
2025-06-05 10:12:52 +08:00

76 lines
2.6 KiB
JavaScript

const express = require('express');
const multer = require('multer');
const path = require('path');
const cors = require('cors');
const fs = require('fs');
const axios = require('axios');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname)));
// Multer setup for file uploads
const upload = multer({
dest: path.join(__dirname, 'uploads/')
});
// File upload endpoint
app.post('/api/upload', upload.single('file'), async (req, res) => {
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded' });
}
// TODO: Integrate with Flowise API for summarization/quiz
// Example: send file to Flowise and get summary/quiz
// const flowiseResponse = await axios.post('FLOWISE_API_URL', ...);
res.json({
message: 'File uploaded successfully',
filename: req.file.filename,
originalname: req.file.originalname
});
});
// Placeholder endpoint for summarization/quiz (to be implemented)
app.post('/api/flowise', async (req, res) => {
// Simulate Flowise integration: read uploaded file and return a fake summary
const { filename } = req.body;
if (!filename) {
return res.status(400).json({ error: 'Filename required' });
}
const filePath = path.join(__dirname, 'uploads', filename);
try {
const fileContent = fs.readFileSync(filePath, 'utf8');
// TODO: Replace with actual Flowise API call
// const flowiseResponse = await axios.post('FLOWISE_API_URL', { content: fileContent });
// return res.json({ summary: flowiseResponse.data.summary });
res.json({ summary: `Sample summary for file: ${filename}\n\n${fileContent.substring(0, 200)}...` });
} catch (err) {
res.status(500).json({ error: 'Could not read file for summary.' });
}
});
// Chatbot endpoint for chitchat with Flowise ChatGPT
app.post('/api/chat', async (req, res) => {
const { message } = req.body;
if (!message) {
return res.status(400).json({ error: 'Message required' });
}
try {
const flowiseRes = await axios.post(
'https://flowise.suika.cc/api/v1/prediction/5079e3d1-4e79-42f5-9c90-1142d9eede4c',
{ question: message },
{ headers: { 'Content-Type': 'application/json' } }
);
res.json({ reply: flowiseRes.data.text || flowiseRes.data.answer || 'No reply.' });
} catch (err) {
res.status(500).json({ error: 'Failed to get reply from Flowise.' });
}
});
app.listen(PORT, () => {
console.log(`EduCat backend running on http://localhost:${PORT}`);
});