plaster/app.js

50 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-12-18 21:47:33 +00:00
const express = require('express');
const fetch = require('node-fetch');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.static('public'));
2024-12-18 21:53:05 +00:00
// Route to fetch Pastebin raw text data using the Paste ID or URL
app.get('/:pasteId?', async (req, res) => {
let rawUrl;
2024-12-18 21:16:43 +00:00
2024-12-18 21:53:05 +00:00
// If the pasteId is provided directly in the URL (like /uxK7EPux)
if (req.params.pasteId) {
const pasteId = req.params.pasteId;
2024-12-18 21:45:39 +00:00
rawUrl = `https://pastebin.com/raw/${pasteId}`;
}
2024-12-18 21:53:05 +00:00
// If the URL parameter is used (like /fetch-paste?url=https://pastebin.com/...)
else if (req.query.url) {
const url = req.query.url;
if (url.includes('pastebin.com/') && !url.includes('/raw/')) {
const pasteId = url.split('/').pop();
rawUrl = `https://pastebin.com/raw/${pasteId}`;
} else {
rawUrl = url; // Assuming it already has '/raw/'
}
}
if (!rawUrl) {
return res.status(400).json({ error: 'URL or Paste ID is required' });
}
2024-12-18 21:16:43 +00:00
try {
const response = await fetch(rawUrl);
if (!response.ok) {
throw new Error('Failed to fetch data from Pastebin');
}
const text = await response.text();
2024-12-18 21:53:05 +00:00
res.setHeader('Content-Type', 'text/plain'); // Ensure it's treated as plain text
res.send(text); // Send raw text
2024-12-18 21:16:43 +00:00
} catch (error) {
2024-12-18 21:53:05 +00:00
res.status(500).json({ error: error.message });
2024-12-18 21:16:43 +00:00
}
});
2024-12-18 21:47:33 +00:00
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});