Skip to content

Commit

Permalink
Merge pull request #5 from georgeliamcampbell/bug/crash-on-failed-req…
Browse files Browse the repository at this point in the history
…uest

Fix crashing and add basic logging
  • Loading branch information
georgeliamcampbell authored Jan 10, 2025
2 parents 1a477c7 + e3fb855 commit b8cbf2a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 14 additions & 0 deletions controllers/documentsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@ const readability = require('@mozilla/readability');
const { Readability } = readability;

exports.fetch = (request, response) => {
console.log(`${request.method} ${request.originalUrl} ${request.body.url}`);

JSDOM.fromURL(request.body.url).then(dom => {
var reader = new Readability(dom.window.document);
var response_body = reader.parse();

console.log(`200 ${request.originalUrl} ${request.body.url}`);
response.json(response_body);
}, error => {
statusCodeMatch = error.message.match(/Status: (\d+)/);
if (statusCodeMatch) {
statusCode = parseInt(statusCodeMatch[1], 10);
} else {
statusCode = 500;
};

console.log(`${statusCode} ${request.originalUrl} ${request.body.url}`);
response.sendStatus(statusCode);
});
};
4 changes: 2 additions & 2 deletions routers/documentsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ const documentsController = require('../controllers/documentsController');
* properties:
* url:
* type: string
* description: The URL to fetch data from
* description: The URL to fetch content from
* example: http://www.github.com
* required:
* - url
* responses:
* 200:
* description: Success
* 500:
* description: Internal server error
* description: Internal Server error
*/
router.post('/v1/documents/fetch', documentsController.fetch);

Expand Down

0 comments on commit b8cbf2a

Please sign in to comment.