mirror of
https://github.com/gigirassy/plaster.git
synced 2024-12-23 10:29:07 +00:00
Update app.js
This commit is contained in:
parent
fd14c7353d
commit
078fae01c2
36
app.js
36
app.js
@ -6,32 +6,22 @@ const PORT = process.env.PORT || 3000;
|
||||
|
||||
app.use(express.static('public'));
|
||||
|
||||
// Route to fetch Pastebin raw text data using the Paste ID or URL
|
||||
app.get('/:pasteId?', (req, res) => {
|
||||
let rawUrl;
|
||||
// Route to handle raw Pastebin requests
|
||||
app.get('/:pasteId', async (req, res) => {
|
||||
const pasteId = req.params.pasteId;
|
||||
const rawUrl = `https://pastebin.com/raw/${pasteId}`;
|
||||
|
||||
// If the pasteId is provided directly in the URL (like /uxK7EPux)
|
||||
if (req.params.pasteId) {
|
||||
const pasteId = req.params.pasteId;
|
||||
rawUrl = `https://pastebin.com/raw/${pasteId}`;
|
||||
}
|
||||
// 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/'
|
||||
try {
|
||||
const response = await fetch(rawUrl);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch data from Pastebin');
|
||||
}
|
||||
const text = await response.text();
|
||||
res.setHeader('Content-Type', 'text/plain'); // Ensure raw text response
|
||||
res.send(text); // Send the raw paste content
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message }); // Return error in JSON format
|
||||
}
|
||||
|
||||
if (!rawUrl) {
|
||||
return res.status(400).json({ error: 'URL or Paste ID is required' });
|
||||
}
|
||||
|
||||
// Perform the redirect to the raw Pastebin URL
|
||||
res.redirect(rawUrl);
|
||||
});
|
||||
|
||||
// Start the server
|
||||
|
Loading…
Reference in New Issue
Block a user