-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from gipplab/feat/addGrobid
Feat/add grobid
- Loading branch information
Showing
8 changed files
with
295 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const express = require('express'); | ||
const axios = require('axios'); | ||
const FormData = require('form-data'); | ||
const multer = require('multer'); // Import multer | ||
const xml2js = require('xml2js'); | ||
const router = express.Router(); | ||
|
||
const upload = multer({ storage: multer.memoryStorage() }); | ||
|
||
router.post("/", upload.single('file'), async (req, res) => { | ||
// Ensure a file is actually provided | ||
if (!req.file) { | ||
return res.status(400).send('No file uploaded.'); | ||
} | ||
|
||
const formData = new FormData(); | ||
formData.append('input', req.file.buffer, { | ||
filename: req.file.originalname, | ||
contentType: req.file.mimetype, | ||
knownLength: req.file.size, | ||
}); | ||
|
||
try { | ||
const grobidResponse = await axios.post('http://grobid:8070/api/processFulltextDocument', formData, { | ||
headers: { | ||
...formData.getHeaders(), | ||
}, | ||
responseType: 'text', // Changed to 'text' to handle XML response correctly | ||
}); | ||
|
||
// Convert XML response to JSON | ||
xml2js.parseString(grobidResponse.data, (err, result) => { | ||
if (err) { | ||
console.error('Error parsing XML: ', err); | ||
return res.status(500).send('Error parsing XML response'); | ||
} | ||
|
||
res.json(result); | ||
}); | ||
|
||
} catch (error) { | ||
console.error('Error when calling GROBID: ', error); | ||
res.status(500).send('Error when processing the PDF file'); | ||
} | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.