Skip to content

Commit

Permalink
added a short url generator project (#241)
Browse files Browse the repository at this point in the history
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
Git21221 authored Oct 4, 2023
2 parents ea724fd + f7fe13b commit f69d379
Show file tree
Hide file tree
Showing 10 changed files with 1,295 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Url-Shortner/connect.js
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,
}
33 changes: 33 additions & 0 deletions Url-Shortner/controllers/url.js
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,
};
43 changes: 43 additions & 0 deletions Url-Shortner/index.js
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}`));
25 changes: 25 additions & 0 deletions Url-Shortner/models/url.js
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;
Loading

0 comments on commit f69d379

Please sign in to comment.