12345678910111213141516171819202122232425262728293031323334353637 |
- const express = require('express');
- const pdf = require('pdf-parse');
- const fs = require('fs');
- const path = require('path');
- const router = express.Router();
- // POST /loader 路由
- router.post('/loader', async (req, res) => {
- const { url } = req.body; // 从请求体中获取 URL
- if (!url) {
- return res.status(400).json({ error: 'No URL provided' });
- }
- try {
- // 使用 fetch 获取 PDF 文件
- const response = await fetch(url);
- if (!response.ok) {
- throw new Error(`Failed to fetch PDF: ${response.statusText}`);
- }
- const buffer = await response.buffer(); // 获取响应的 buffer
- // 解析 PDF 文件
- const data = await pdf(buffer);
- // 返回解析后的数据
- res.json(data);
- } catch (error) {
- // 处理错误
- res.status(500).json({ error: 'Failed to parse PDF', details: error.message });
- }
- });
- module.exports = router;
|