Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const mongoose = require('mongoose');

const indexRouter = require('./routes/index');

Expand Down Expand Up @@ -36,4 +37,15 @@ app.use(function (err, req, res, next) {
res.render('error');
});

const dbUrl = "mongodb://127.0.0.1:27017/urlDB";

// connect to database
mongoose.connect(dbUrl)
.then(() => {
console.log("Connected to database");
})
.catch((err) => {
console.error(err);
});

module.exports = app;
17 changes: 17 additions & 0 deletions models/urlSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mongoose = require("mongoose");

const urlsc = new mongoose.Schema({
originalUrl: {
type: String,
required: true,
},

shortUrl: {
type: String,
required: true,
},
});

const Url = mongoose.model("shortenedUrl", urlsc);

module.exports = Url;
215 changes: 215 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"ejs": "^3.1.9",
"express": "^4.18.2",
"http-errors": "~1.6.3",
"mongoose": "^8.4.0",
"morgan": "~1.9.1"
},
"devDependencies": {
Expand Down
41 changes: 40 additions & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,47 @@
const router = require('express').Router();

const urlSchema = require('../models/urlSchema.js'); // get the schema of url

/*
router.get('/favicon.ico', async function (req, res,next) {
const urls = await urlSchema.find({});
res.locals.allUrl = urls;
res.render('index', { title: 'Express' });
});
*/

// get website using its alias Url
router.get('/:aliasUrl', async (req, res) => {
const urlCollection = await urlSchema.findOne({shortUrl : req.params.aliasUrl});
if(urlCollection){
console.log(urlCollection);
const link = urlCollection.originalUrl;
res.redirect(link);
}
else{
res.status(404).send('Not Found');
}
});

/* GET home page. */
router.get('/', function (req, res, next) {
router.get('/', async function (req, res,next) {
const urls = await urlSchema.find({});
res.locals.allUrl = urls;
res.render('index', { title: 'Express' });
});

// Add new url and its Alias
router.post('/', async function (req, res) {

const newUrlShortener = new urlSchema({

originalUrl: req.body.urlInput,
shortUrl: req.body.aliasInput

});

await newUrlShortener.save();
res.redirect('/');
});

module.exports = router;
Loading