-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
35 lines (27 loc) · 782 Bytes
/
index.js
File metadata and controls
35 lines (27 loc) · 782 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
31
32
33
34
const express = require('express');
const urlRoute = require('./routes/url');
const URL = require('./models/url');
const { connectDB } = require('./connect');
const app = express();
const PORT = 8001;
connectDB('mongodb://localhost:27017/url-shortener').then(() => {
console.log("Connected to DB");
});
app.use(express.json());
app.use("/url", urlRoute)
app.get('/:shortId', async (req, res) => {
const shortId = req.params.shortId;
const entry = await URL.findOneAndUpdate(
{
shortId
},
{
$push: {
visitHistory: {
timestamp: Date.now(),
},
}
});
res.redirect(entry.redirectUrl);
});
app.listen(PORT, () => console.log(`Server is running on port ${PORT}`));