@@ -15,6 +15,8 @@ const JWT_SECRET = 'super-secret-key-pdf-preview';
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
app.use('/uploads/processed', express.static(path.join(__dirname, 'uploads/processed')));
|
||||
// Serve the built frontend from the public folder
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
|
||||
// Ensure upload directories exist
|
||||
const uploadDirOrig = path.join(__dirname, 'uploads/original');
|
||||
@@ -24,7 +26,8 @@ if (!fs.existsSync(uploadDirProc)) fs.mkdirSync(uploadDirProc, { recursive: true
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: function (req, file, cb) {
|
||||
cb(null, 'uploads/original/');
|
||||
// 使用绝对路径,避免服务器 CWD 不在项目目录时写入失败
|
||||
cb(null, path.join(__dirname, 'uploads/original/'));
|
||||
},
|
||||
filename: function (req, file, cb) {
|
||||
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
|
||||
@@ -79,7 +82,13 @@ app.post('/api/admin/upload', authenticateJWT, upload.single('document'), async
|
||||
|
||||
res.json({ message: 'Upload successful', documentId: info.lastInsertRowid });
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: 'Error processing PDF', error: error.toString() });
|
||||
// 输出完整堆栈到服务器日志,便于排查
|
||||
console.error('Upload error details:', error.stack || error);
|
||||
res.status(500).json({
|
||||
message: 'Error processing PDF',
|
||||
error: error.message || error.toString(),
|
||||
stack: process.env.NODE_ENV !== 'production' ? error.stack : undefined
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -105,7 +114,7 @@ app.get('/api/documents', (req, res) => {
|
||||
id: doc.id,
|
||||
title: doc.title,
|
||||
created_at: doc.created_at,
|
||||
url: `http://localhost:3000/${doc.processed_path}`
|
||||
url: `/${doc.processed_path}`
|
||||
}));
|
||||
res.json(responseData);
|
||||
});
|
||||
@@ -119,10 +128,22 @@ app.get('/api/documents/:id', (req, res) => {
|
||||
id: doc.id,
|
||||
title: doc.title,
|
||||
created_at: doc.created_at,
|
||||
url: `http://localhost:3000/${doc.processed_path}`
|
||||
url: `/${doc.processed_path}`
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Backend server running on http://localhost:${PORT}`);
|
||||
});
|
||||
|
||||
// SPA fallback: must be after all API routes so Vue Router can handle client-side routing
|
||||
// Note: app.use() (not app.get('*')) is required for Express 5 compatibility
|
||||
app.use((req, res) => {
|
||||
const indexPath = path.join(__dirname, 'public', 'index.html');
|
||||
if (fs.existsSync(indexPath)) {
|
||||
res.sendFile(indexPath);
|
||||
} else {
|
||||
res.status(404).send('Frontend not built yet. Run: cd frontend && npm run build');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user