-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (29 loc) · 1.02 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
// function mainApp() {
const express = require("express");
const urlRoute = require("./routes/url");
const { connectMongo } = require("./connect");
const URL = require("./models/url-model");
const app = express();
const PORT = 8000;
app.use(express.json());
app.set("view engine", "ejs");
connectMongo("mongodb://127.0.0.1:27017/url-shortener")
.then(() => console.log("mongodb connected"))
.catch((err) => console.log("Could not connect to mongo", err));
app.use("/url", urlRoute);
app.get("/:shortId", async (req, res) => {
// console.log(req.params.shortId);
try {
const data = await URL.findOneAndUpdate(
{ shortId: req.params.shortId },
{ $push: { visitHistory: { timeStamp: Date.now() } } }
);
// res.json({ "redirect-url": data["redirectURL"] });
res.redirect(`https://${data.redirectURL}`);
} catch (error) {
console.log("An error occured while redirecting | error-message:", error);
}
});
app.listen(PORT, () => console.log("listening on", PORT));
// }
// module.exports = mainApp;