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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
node_modules/
.env
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Fork this repository and use the given files to start with.
## Idea

A URL shortener website is a service that converts long website addresses into shorter, more manageable links. Users input a lengthy URL, and the website generates a condensed version, making it easier to share and remember.

## Demo
[Demo]: https://url-shortener-7eyn.onrender.com
[Demo][Demo]
## Interface

The application interface consists of one page which contains:
Expand Down Expand Up @@ -43,4 +45,4 @@ http://localhost:3000/{alias}
* Project Completeness (25 pts.)
* Clean Code and Modulation (15 pts.)
* Descriptive Git Commit Messages (10 pts.)
* Nice touches (5 pts. bonus)
* Nice touches (5 pts. bonus)
9 changes: 9 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
const createError = require('http-errors');
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
require('dotenv').config();

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

const app = express();

//connect to mongoDB
const dbURI = process.env.DB_URI;
mongoose.connect(dbURI)
.then((result) => app.listen(3000))
.catch((err) => console.log(err));

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
Expand All @@ -20,6 +28,7 @@ app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);


// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
Expand Down
57 changes: 57 additions & 0 deletions controllers/urlController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const URL = require('../models/url');

const getHome = (req, res) => {
URL.find()
.then((urls) => {
res.render('index', { title: 'urlShortner', urls });
})
.catch((err) => {
res.render('error', { message: "Failed to load home page", error: err } );
})
};

const urlPost = (req, res) => {
const long = req.body.urlInput;
const alias = req.body.aliasInput;
const url = new URL({ long, alias});
url.save()
.then(() => {
res.redirect('/');
console.log('a new link added')
})
.catch((err) => {
res.render('error', { message: "Failed to load home page", error: err });
})
}
const urlRedirect = (req, res) => {
const alias = req.params.alias;
URL.findOne({ alias })
.then((url) => {
if (url) {
res.redirect(`${url.long}`);
}
else {
res.render('error', { message: "Invalid alias", error: err });
}
})
.catch((err) => {
res.render('error', { message: "Failed to redirect", error: err });
})
}

const urlDelete = (req, res) => {
const id = req.params.id;
URL.findByIdAndDelete(id)
.then(result => {
res.json({ redirect: '/'})
})
.catch((err) => {
res.render('error', { message: "Failed to load home page", error: err }); })
}

module.exports = {
getHome,
urlPost,
urlDelete,
urlRedirect,
}
10 changes: 10 additions & 0 deletions models/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const urlSchema = new Schema({
long: { type: String, required: true },
alias: { type: String, required: true }
}, { timestamps: true })

const URL = mongoose.model('URL', urlSchema);
module.exports = URL;
Loading