-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (65 loc) · 1.94 KB
/
Copy pathindex.js
File metadata and controls
80 lines (65 loc) · 1.94 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
74
75
76
77
78
79
80
const express = require("express");
const { connectToMongoDB } = require("./connect");
const urlRoute = require('./Routes/url');
const path= require('path');
const staticRoute =require('./Routes/staticRouter');
const URL = require('./models/url');
const app = express();
const PORT = 8001;
connectToMongoDB('mongodb://127.0.0.1:27017/short-url')
.then(() => console.log("MONGODB connected"));
app.set("view engine", "ejs");
app.set('views', path.resolve("./views"));
app.use(express.json());
app.use(express.urlencoded({ extended:false}))
app.use("/url", urlRoute);
app.use("/", staticRoute);
app.get('/test', async (req, res) => {
const allUrls = await URL.find({});
return res.render('home',{
urls:allUrls,
});
});
app.get('/url/:shortId', async (req, res) => {
const shortId = req.params.shortId;
const entry = await URL.findOneAndUpdate(
{ shortId },
{
$push: {
visitHistory: {
timestamp: Date.now(),
},
},
},
{ new: true } // Ensures the updated document is returned
);
if (!entry) {
return res.status(404).json({ error: 'URL not found' });
}
res.redirect(entry.redirectURL);
});
app.listen(PORT, () => console.log(`Server Started at PORT: ${PORT}`));
// 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://127.0.0.1:27017/short-url')
// .then(()=>console.log("MONGODB connected"))
// 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 Started at PORT:${PORT}`));