-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
30 lines (26 loc) · 832 Bytes
/
server.js
File metadata and controls
30 lines (26 loc) · 832 Bytes
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
const express = require('express');
const mongoose = require('mongoose');
const URL = require('./models/url.js');
const app = express();
const PORT = 8001;
// Connect to MongoDB
mongoose.connect('mongodb://127.0.0.1:27017/discord-shorturl')
.then(() => console.log('MongoDB Connected for Server'))
.catch(err => console.error('MongoDB connection error:', err));
// Redirect route
app.get('/url/:shortId', async (req, res) => {
try {
const url = await URL.findOne({ shortID: req.params.shortId });
if (url) {
res.redirect(url.redirectURL);
} else {
res.status(404).send('URL not found');
}
} catch (error) {
console.error('Redirect error:', error);
res.status(500).send('Server error');
}
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});