From 27d430d01710c9b3ad9aff806a74b7d22d08c0af Mon Sep 17 00:00:00 2001 From: nune <145225213+gigirassy@users.noreply.github.com> Date: Wed, 18 Dec 2024 16:45:39 -0500 Subject: [PATCH] Update app.js --- app.js | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/app.js b/app.js index 6f27edb..afd8a39 100644 --- a/app.js +++ b/app.js @@ -1,17 +1,15 @@ -const express = require('express'); -const fetch = require('node-fetch'); +app.get('/fetch-paste', async (req, res) => { + const { url } = req.query; + if (!url) { + return res.status(400).json({ error: 'URL is required' }); + } -const app = express(); -const PORT = process.env.PORT || 3000; - -app.use(express.static('public')); - -// Route to fetch Pastebin raw text data using the Paste ID -app.get('/:pasteId', async (req, res) => { - const pasteId = req.params.pasteId; - - // Construct the raw Pastebin URL - const rawUrl = `https://pastebin.com/raw/${pasteId}`; + // Check if the URL is a Pastebin URL and convert to raw format + let rawUrl = url; + if (url.includes('pastebin.com/') && !url.includes('/raw/')) { + const pasteId = url.split('/').pop(); + rawUrl = `https://pastebin.com/raw/${pasteId}`; + } try { const response = await fetch(rawUrl); @@ -19,13 +17,8 @@ app.get('/:pasteId', async (req, res) => { throw new Error('Failed to fetch data from Pastebin'); } const text = await response.text(); - res.send(`
${text}
`); + res.json({ text }); // Always send JSON } catch (error) { - res.status(500).send(`Error: ${error.message}`); + res.status(500).json({ error: error.message }); // Ensure error is JSON } }); - -// Start the server -app.listen(PORT, () => { - console.log(`Server is running on http://localhost:${PORT}`); -});