-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a short url generator project (#241)
Fixes #123 short url generator project is completed ! #Before <img width="365" alt="Screenshot 2023-10-03 210041" src="https://github.com/Git21221/JS-beginner-projects/assets/119128989/2a1c407a-8134-42d9-8cf5-cf3e6eb72f29"> #After <img width="304" alt="Screenshot 2023-10-03 210255" src="https://github.com/Git21221/JS-beginner-projects/assets/119128989/d041f427-4387-4985-aa3f-03c04d9f82af">
- Loading branch information
Showing
10 changed files
with
1,295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const mongoose=require("mongoose") | ||
async function connectToMongoDB(url){ | ||
return mongoose.connect(url); | ||
} | ||
module.exports={ | ||
connectToMongoDB, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const shortid = require("shortid"); | ||
const URL = require("../models/url"); | ||
|
||
async function handleGenerateNewShortURL(req, res) { | ||
const body = req.body; | ||
if (!body.url) return res.status(400).json({ error: "url is required" }); | ||
const shortID = shortid(); | ||
|
||
await URL.create({ | ||
shortId: shortID, | ||
redirectURL: body.url, | ||
visitHistory: [], | ||
|
||
}); | ||
|
||
return res.render("home", { | ||
id: shortID, | ||
}); | ||
} | ||
|
||
async function handleAnalytics(req, res) { | ||
const shortId = req.params.shortId; | ||
const result = await URL.findOne({ shortId }); | ||
return res.json({ | ||
totalClicks: result.visitHistory.length, | ||
analytics: result.visitHistory, | ||
}); | ||
} | ||
|
||
module.exports = { | ||
handleGenerateNewShortURL, | ||
handleAnalytics, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const express = require("express"); | ||
const path = require("path"); | ||
const cookieParser = require("cookie-parser"); | ||
const { connectToMongoDB } = require("./connect"); | ||
const URL = require("./models/url"); | ||
const urlRoute = require("./routes/url"); | ||
const staticRoute = require("./routes/staticRouter"); | ||
const app = express(); | ||
const PORT = 3000; | ||
|
||
connectToMongoDB(process.env.MONGODB ?? "mongodb://localhost: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(cookieParser()); | ||
|
||
app.use("/url", urlRoute); | ||
|
||
app.use("/", staticRoute); | ||
|
||
app.get("/url/: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}`)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const urlSchema = new mongoose.Schema( | ||
{ | ||
shortId: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
}, | ||
redirectURL: { | ||
type: String, | ||
required: true, | ||
}, | ||
visitHistory: [{ timestamp: { type: Number } }], | ||
createdBy: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "users", | ||
}, | ||
}, | ||
{ timestamps: true } | ||
); | ||
|
||
const URL = mongoose.model("url", urlSchema); | ||
|
||
module.exports = URL; |
Oops, something went wrong.