-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
73 lines (60 loc) · 2.09 KB
/
script.js
File metadata and controls
73 lines (60 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// server.js
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const { nanoid } = require('nanoid');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static('public')); // Serve static files from the 'public' directory
// MongoDB connection
mongoose.connect('mongodb+srv://romeobot555:mdraselm325@cluster0.ue47qhn.mongodb.net/pastebin?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log('MongoDB connected successfully'))
.catch(err => console.error('MongoDB connection error:', err));
// Paste schema
const pasteSchema = new mongoose.Schema({
urlId: { type: String, required: true, unique: true },
content: { type: String, required: true },
});
const Paste = mongoose.model('Paste', pasteSchema);
// POST route to save paste
app.post('/save', async (req, res) => {
try {
const { text } = req.body;
if (!text) {
console.log('No text provided');
return res.status(400).json({ error: 'Text content is required.' });
}
console.log('Received text:', text);
const urlId = nanoid(7); // Generate a random 7-character ID
const newPaste = new Paste({ urlId, content: text });
await newPaste.save();
console.log('Paste saved successfully:', newPaste);
res.json({ url: `http://localhost:${PORT}/view/${urlId}` });
} catch (error) {
console.error('Error saving paste:', error.message);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// GET route to view paste
app.get('/view/:urlId', async (req, res) => {
try {
const paste = await Paste.findOne({ urlId: req.params.urlId });
if (!paste) {
return res.status(404).send('Paste not found');
}
res.send(`<pre>${paste.content}</pre>`);
} catch (error) {
console.error('Error fetching paste:', error.message);
res.status(500).send('Internal Server Error');
}
});
// Start server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});