-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (47 loc) · 1.58 KB
/
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const express = require('express');
const path = require('path');
const {connectToMongoDB} = require("./dbconnect");
const urlRoute = require("./routes/url");
const staticRoute = require('./routes/staticRouter');
const URL = require('./model/url');
const dotenv = require('dotenv');
dotenv.config();
const app = express();
const MONGODB_URI = process.env.MONGODB_URI;
const PORT = process.env.PORT || 8001;
connectToMongoDB(MONGODB_URI)
.then(() => console.log('MongoDB Connected'))
.catch((error) => console.error('MongoDB Connection Error:', error));
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("/url/:shortId", async (req, res) => {
const shortId = req.params.shortId;
try {
const entry = await URL.findOneAndUpdate(
{
shortId,
},
{
$push: {
visitHistory: {
timestamps: Date.now(),
},
},
},
);
if (entry && entry.redirectURL) {
res.redirect(entry.redirectURL);
} else {
// Handle the case when entry is null or redirectURL is not defined
res.status(404).send("URL not found");
}
} catch (error) {
console.error("Error finding URL:", error);
res.status(500).send("Internal Server Error");
}
});
app.listen(PORT, () => console.log(`Server started at PORT ${PORT}`));