Skip to content

Commit 7c4cf2d

Browse files
committed
minor fixes
1 parent 4870831 commit 7c4cf2d

20 files changed

Lines changed: 142 additions & 241 deletions

backend/app.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
const express = require('express');
2-
const app = express();
31
require('dotenv').config();
2+
const express = require('express');
43
const cookieParser = require('cookie-parser');
54
const path = require('path');
65
const connectToMongoDB = require('./connection.js');
76
const passport = require('passport');
8-
const cors = require("cors");
97

10-
app.use(cors());
8+
const app = express();
119
app.use(express.json());
1210
app.use(express.urlencoded({ extended: false }));
1311
app.use(cookieParser());
@@ -21,11 +19,6 @@ app.use(passport.initialize());
2119
app.set('view engine', 'ejs');
2220
app.set('views', path.resolve("./views"));
2321

24-
app.use((req, res, next) => {
25-
console.log(req.method, req.url, req.body);
26-
next();
27-
});
28-
2922
const urlRoutes = require('./routes/url.routes.js');
3023
const userRoutes = require('./routes/user.routes.js');
3124

backend/controllers/createUrl.controllers.js

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,68 @@ const Store_Url = require('../models/url.models.js');
22
const shortid = require('shortid');
33
const validator = require('validator');
44

5-
async function createShortUrl(req, res) {
5+
module.exports.createShortUrl = async (req, res) => {
66

77
let { url, alias } = req.body;
8+
89
if (!url) return res.status(404).json({ message: "Please provide a url" });
10+
911
url = url.trim();
12+
1013
if (!validator.isURL(url)) {
1114
return res.status(400).json({ message: "Invalid URL..." });
1215
}
1316

1417
alias = alias ? alias.trim() : null;
1518

16-
if (alias) {
17-
let urlExists = await Store_Url.findOne({ shortId: alias });
19+
try {
20+
if (alias) {
21+
let urlExists = await Store_Url.findOne({ shortId: alias });
1822

19-
if (urlExists) {
20-
return res.status(400).json({ message: "Alias is already taken" });
21-
} else {
22-
let targeturl = /^https?:\/\//i.test(url) ? url : `https://${url}`;
23+
if (urlExists) {
24+
return res.status(400).json({ message: "Alias is already taken" });
25+
} else {
26+
let targeturl = /^https?:\/\//i.test(url) ? url : `https://${url}`;
2327

24-
await Store_Url.create({
25-
shortId: alias,
26-
redirectUrl: targeturl,
27-
visitHistory: [],
28-
owner: req.user.id,
29-
name: getDomainWithoutTLD(targeturl)
30-
});
28+
await Store_Url.create({
29+
shortId: alias,
30+
redirectUrl: targeturl,
31+
visitHistory: [],
32+
owner: req.user.id,
33+
name: getDomainWithoutTLD(targeturl)
34+
});
3135

32-
return res.status(200).json({ new: alias });
36+
return res.status(200).json({ new: alias });
37+
}
3338
}
34-
}
3539

36-
let rand = shortid.generate().slice(0, 7);
40+
let rand = shortid.generate().slice(0, 7);
3741

38-
let urlExists = await Store_Url.findOne({ shortId: rand });
39-
while (urlExists) {
40-
rand = shortid.generate().slice(0, 7);
41-
urlExists = await Store_Url.findOne({ shortId: rand });
42-
}
43-
let targeturl = /^https?:\/\//i.test(url) ? url : `https://${url}`;
42+
let urlExists = await Store_Url.findOne({ shortId: rand });
43+
while (urlExists) {
44+
rand = shortid.generate().slice(0, 7);
45+
urlExists = await Store_Url.findOne({ shortId: rand });
46+
}
47+
let targeturl = /^https?:\/\//i.test(url) ? url : `https://${url}`;
48+
49+
await Store_Url.create({
50+
shortId: rand,
51+
redirectUrl: targeturl,
52+
visitHistory: [],
53+
owner: req.user.id,
54+
name: getDomainWithoutTLD(targeturl)
55+
});
4456

45-
await Store_Url.create({
46-
shortId: rand,
47-
redirectUrl: targeturl,
48-
visitHistory: [],
49-
owner: req.user.id,
50-
name: getDomainWithoutTLD(targeturl)
51-
});
57+
res.status(200).json({ new: rand });
5258

53-
res.status(200).json({ new: rand });
59+
} catch (error) {
60+
console.error(error);
61+
res.status(500).json({ error: "Something went wrong please try again later." });
62+
}
5463
}
5564

5665
function getDomainWithoutTLD(url) {
5766
const hostname = new URL(url).hostname.replace("www.", "");
5867
const parts = hostname.split(".");
5968
return parts.length > 2 ? parts.slice(0, 2).join(".") : parts[0];
60-
}
61-
62-
module.exports = createShortUrl;
69+
}
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
const Store_Url = require('../models/url.models.js');
22
const mongoose = require('mongoose');
33

4-
async function getDetails(req, res) {
5-
4+
module.exports.getDetails = async (req, res) => {
65
let shorturl = req.query.url;
76

8-
const url = await Store_Url.findOne({ shortId: shorturl })
7+
try {
8+
const url = await Store_Url.findOne({ shortId: shorturl })
99

10-
if (url) {
11-
if (url.owner.equals(new mongoose.Types.ObjectId(req.user.id))) {
12-
return res.status(200).json({
13-
url: url.redirectUrl,
14-
lastOpened: url.visitHistory
15-
})
16-
} else {
17-
return res.status(404).json({ error: "Sorry, But This url is not owned by you" });
10+
if (url) {
11+
if (url.owner.equals(new mongoose.Types.ObjectId(req.user.id))) {
12+
return res.status(200).json({
13+
url: url.redirectUrl,
14+
lastOpened: url.visitHistory
15+
});
16+
} else {
17+
return res.status(404).json({ error: "Sorry, But This url is not owned by you" });
18+
}
1819
}
20+
res.status(404).json({ error: "Short url not exists." });
21+
} catch (error) {
22+
console.error(error);
23+
res.status(500).json({ error: "Something went wrong, please try again later." });
1924
}
20-
21-
res.status(404).json({ error: "Short url not exists." });
2225
}
2326

24-
async function getHomePageUrls(req, res) {
27+
module.exports.getHomePageUrls = async (req, res) => {
28+
const userId = req.user.id;
2529
try {
26-
const userId = req.user.id;
2730
const urls = await Store_Url.find({ owner: userId }).sort({ createdAt: -1 }).limit(5);
2831
res.json(urls);
2932
} catch (error) {
30-
res.status(500).json({ error: "Failed to fetch URLs" });
33+
console.error(error);
34+
res.status(500).json({ error: "Something went wrong, please try again later." });
3135
}
32-
};
33-
34-
module.exports = { getDetails, getHomePageUrls }
36+
}

backend/controllers/redirectUrl.controllers.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const Store_Url = require('../models/url.models.js');
22
const useragent = require('useragent');
3-
async function redirectUrl(req, res, next) {
4-
3+
4+
module.exports.redirectUrl = async (req, res, next) => {
55
try {
66
const url = await Store_Url.findOne({ shortId: req.params.p });
77

@@ -42,6 +42,4 @@ async function redirectUrl(req, res, next) {
4242
console.error('Error in redirectUrl:', error);
4343
next();
4444
}
45-
}
46-
47-
module.exports = { redirectUrl }
45+
}
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
const Store_Url = require('../models/url.models.js');
22

3-
async function userUrls(req, res) {
4-
3+
module.exports.userUrls = async (req, res) => {
54
const userId = req.user.id;
65
try {
76
const urls = await Store_Url.find({ owner: userId }).sort({ createdAt: -1 });
87
res.status(200).json(urls);
98
} catch (error) {
9+
console.error(error);
1010
res.status(500).json({ error: "Failed to fetch URLs" });
1111
}
1212
}
1313

14-
async function deleteUrl(req, res){
14+
module.exports.deleteUrl = async (req, res) => {
1515
const { p } = req.params;
1616
try {
17-
1817
const deletedUrl = await Store_Url.findOneAndDelete({ owner: req.user.id, shortId: p });
1918

2019
if (!deletedUrl) {
2120
return res.status(404).json({ error: "URL not found or unauthorized" });
2221
}
2322

24-
return res.status(200).json({ message: "URL deleted successfully", deletedUrl });
25-
23+
res.status(200).json({ message: "URL deleted successfully", deletedUrl });
2624
} catch (error) {
25+
console.error(error);
2726
res.status(500).json({ message: "Failed to delete URL", error });
2827
}
29-
}
30-
31-
module.exports = {userUrls, deleteUrl};
28+
}

backend/controllers/user.controllers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports.handleUserLoginWithPassword = async (req, res) => {
3535
res.status(200).json({
3636
token,
3737
message: "Login successful"
38-
})
38+
});
3939
} catch (error) {
4040
console.error(error);
4141
return res.status(500).json({ message: "Something went wrong, please try again later" });

backend/middlewares/auth.middlewares.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { getUser } = require('../services/auth.services.js');
22

3-
async function restrictToUserLogin(req, res, next) {
3+
module.exports.restrictToUserLogin = async (req, res, next) => {
44
const uid = req.cookies?.uid;
55
const authHeader = req.headers['authorization'] || req.headers['Authorization'];
66
try {
@@ -34,7 +34,7 @@ async function restrictToUserLogin(req, res, next) {
3434
}
3535
}
3636

37-
async function restrictToLoginedUser(req, res, next) {
37+
module.exports.restrictToLoginedUser = (req, res, next) => {
3838
const token = req.cookies?.uid;
3939
if (!token)
4040
return next();
@@ -43,6 +43,4 @@ async function restrictToLoginedUser(req, res, next) {
4343
return next();
4444

4545
return res.redirect('/');
46-
}
47-
48-
module.exports = { restrictToUserLogin, restrictToLoginedUser };
46+
}

backend/models/url.models.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,4 @@ const urlSchema = new mongoose.Schema({
3333
}]
3434
}, { timestamps: true })
3535

36-
const Store_Url = mongoose.model("url", urlSchema);
37-
38-
module.exports = Store_Url;
36+
module.exports = mongoose.model("url", urlSchema);

backend/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"main": "app.js",
55
"scripts": {
66
"start": "node app.js",
7-
"dev": "nodemon app.js"
7+
"dev": "node --watch app.js",
8+
"bun": "bun app.js",
9+
"build:css": "npx tailwindcss -i ./src/input.css -o ./public/output.css --watch"
810
},
911
"keywords": [],
1012
"author": "",
@@ -27,7 +29,6 @@
2729
"validator": "^13.12.0"
2830
},
2931
"devDependencies": {
30-
"nodemon": "^3.1.9",
3132
"tailwindcss": "^3.4.17"
3233
}
3334
}

0 commit comments

Comments
 (0)