-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
35 lines (26 loc) · 824 Bytes
/
index.js
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
const express = require('express');
const { connectToMongoDB } = require('./connect');
const urlRoute = require("./routes/url");
const URL = require('./models/url');
const app = express();
const PORT = 8001;
connectToMongoDB('mongodb://localhost:27017/short-url')
.then(() => console.log('Mongodb connect'));
app.use(express.json());
app.use('/url', urlRoute);
app.get('/:shortId', async (req, res) => {
const {shortId }= req.params;
const entry = await URL.findOneAndUpdate({
shortId
},
{
$push: {
visitHistory: {
timestamp: Date.now(),
},
},
}
);
res.redirect(entry.redirectURL);
});
app.listen(PORT, () => console.log(`Server Started at PORT: ${PORT}`));