Update app.js

This commit is contained in:
nune 2024-12-18 16:45:39 -05:00 committed by GitHub
parent 657035b410
commit 27d430d017
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

33
app.js
View File

@ -1,17 +1,15 @@
const express = require('express'); app.get('/fetch-paste', async (req, res) => {
const fetch = require('node-fetch'); const { url } = req.query;
if (!url) {
return res.status(400).json({ error: 'URL is required' });
}
const app = express(); // Check if the URL is a Pastebin URL and convert to raw format
const PORT = process.env.PORT || 3000; let rawUrl = url;
if (url.includes('pastebin.com/') && !url.includes('/raw/')) {
app.use(express.static('public')); const pasteId = url.split('/').pop();
rawUrl = `https://pastebin.com/raw/${pasteId}`;
// 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}`;
try { try {
const response = await fetch(rawUrl); const response = await fetch(rawUrl);
@ -19,13 +17,8 @@ app.get('/:pasteId', async (req, res) => {
throw new Error('Failed to fetch data from Pastebin'); throw new Error('Failed to fetch data from Pastebin');
} }
const text = await response.text(); const text = await response.text();
res.send(`<pre>${text}</pre>`); res.json({ text }); // Always send JSON
} catch (error) { } 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}`);
});