Skip to content

Commit 0e753cd

Browse files
URL-Shortener
0 parents  commit 0e753cd

File tree

2,056 files changed

+329904
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,056 files changed

+329904
-0
lines changed

connect.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const mongoose = require('mongoose');
2+
3+
async function connectToMongoDB(url) {
4+
return mongoose.connect(url);
5+
}
6+
7+
module.exports = {
8+
connectToMongoDB,
9+
}

controllers/url.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const shortid = require('shortid');
2+
const URL = require('../models/url');
3+
async function handleGenerateNewShortURL(req, res){
4+
const body = req.body;
5+
if(!body.url) return res.status(400).json({ error: 'url is required'})
6+
const shortID = shortid(8);
7+
await URL.create({
8+
shortId: shortID,
9+
redirectURL: body.url,
10+
visitHistory: [],
11+
12+
});
13+
14+
return res.render('home', {
15+
id: shortID
16+
});
17+
}
18+
19+
async function handleGetAnalytics(req, res){
20+
const shortId = req.params.shortId;
21+
const result = await URL.findOne({ shortId });
22+
return res.json({
23+
totalClicks: result.visitHistory.length, analytics:visitHistory
24+
});
25+
}
26+
27+
module.exports = {
28+
handleGenerateNewShortURL,
29+
handleGetAnalytics
30+
}

index.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const express = require('express');
2+
const path = require('path');
3+
const staticRouter = require('./routes/staticRouter');
4+
const { connectToMongoDB } = require('./connect');
5+
const urlRoute = require('./routes/url');
6+
const URL = require('./models/url');
7+
const app = express();
8+
const PORT = 8000;
9+
10+
connectToMongoDB('mongodb://localhost:27017/short-url').then(() =>
11+
console.log('Mongodb Connected')
12+
);
13+
14+
app.set('view engine', 'ejs');
15+
app.set('views', path.resolve('./views'));
16+
17+
// middleware
18+
app.use(express.json());
19+
app.use(express.urlencoded({ extended: false }));
20+
app.use('/url', urlRoute);
21+
app.use('/', staticRouter);
22+
23+
app.get('/url/:shortId', async (req, res) => {
24+
const shortId = req.params.shortId;
25+
const entry = await URL.findOneAndUpdate(
26+
{
27+
shortId,
28+
},
29+
{
30+
$push: {
31+
visitHistory: {
32+
timestamp: Date.now(),
33+
},
34+
},
35+
}
36+
);
37+
res.redirect(entry.redirectURL);
38+
});
39+
app.listen(PORT, () => console.log(`Server is running on PORT:${PORT}`));

models/url.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const mongoose = require('mongoose');
2+
const urlSchema = new mongoose.Schema({
3+
shortId: {
4+
type: String,
5+
required: true,
6+
unique: true,
7+
},
8+
redirectURL: {
9+
type: String,
10+
required: true,
11+
},
12+
visitHistory: [{ timestamp: { type: Number }}],
13+
},
14+
{ timestamps: true }
15+
);
16+
17+
const URL = mongoose.model("url", urlSchema);
18+
module.exports = URL;

node_modules/.bin/ejs

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/ejs.cmd

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/ejs.ps1

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/jake

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/jake.cmd

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/jake.ps1

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/mime

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/mime.cmd

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/mime.ps1

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/nanoid

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/nanoid.cmd

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/nanoid.ps1

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/nodemon

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/nodemon.cmd

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)