initial done
This commit is contained in:
24
model.js
Normal file
24
model.js
Normal file
@@ -0,0 +1,24 @@
|
||||
const fs = require("fs");
|
||||
|
||||
let urls = require("./model.json");
|
||||
|
||||
function save(url, shortened) {
|
||||
urls.push({url: url, shortened: shortened});
|
||||
commit();
|
||||
}
|
||||
|
||||
function commit() {
|
||||
fs.writeFile("./model.json", JSON.stringify(urls));
|
||||
}
|
||||
|
||||
function search(key) {
|
||||
for (let model of urls) {
|
||||
if (key == model.shortened) return model;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
save: save,
|
||||
search: search,
|
||||
}
|
1
model.json
Normal file
1
model.json
Normal file
@@ -0,0 +1 @@
|
||||
[]
|
@@ -15,7 +15,6 @@
|
||||
"dependencies": {
|
||||
"hapi": "^14.1.0",
|
||||
"inert": "^4.0.1",
|
||||
"joi": "^9.0.4",
|
||||
"mongoose": "^4.5.8"
|
||||
"joi": "^9.0.4"
|
||||
}
|
||||
}
|
||||
|
@@ -4,9 +4,9 @@ var link = document.getElementById('link');
|
||||
var shrBox = document.getElementById('shortened');
|
||||
|
||||
function displayShortenedUrl(response) {
|
||||
link.textContent = response.data.shortUrl;
|
||||
link.textContent = response.data.url;
|
||||
link.setAttribute(
|
||||
'href', response.data.shortUrl
|
||||
'href', response.data.url
|
||||
);
|
||||
shrBox.style.opacity = '1';
|
||||
urlBox.value = '';
|
||||
|
@@ -1,5 +0,0 @@
|
||||
#shorten-form {
|
||||
width: 300px;
|
||||
margin: auto;
|
||||
margin-top: 10vh;
|
||||
}
|
30
routes.js
30
routes.js
@@ -1,16 +1,9 @@
|
||||
const Joi = require('joi');
|
||||
const mongoose = require('mongoose');
|
||||
const Schema = mongoose.Schema;
|
||||
const hash = require('./hash');
|
||||
const model = require('./model');
|
||||
const HASHLEN = 7;
|
||||
|
||||
const baseUrl = process.env.BASE_URL || 'shrt.in';
|
||||
|
||||
const schema = new Schema({
|
||||
shrt: String,
|
||||
url: String,
|
||||
});
|
||||
const Shrt = mongoose.model('Shrt', schema);
|
||||
const baseUrl = process.env.BASE_URL || 'http://ve.it';
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
@@ -32,12 +25,10 @@ module.exports = [
|
||||
path: '/new',
|
||||
handler(request, reply) {
|
||||
const uid = hash(HASHLEN);
|
||||
const r = new Shrt({
|
||||
short: `${baseUrl}/${uniqueID}`,
|
||||
url: request.payload.url,
|
||||
});
|
||||
const url = `${baseUrl}/${uid}`;
|
||||
model.save(request.payload.url, url);
|
||||
|
||||
r.save((err, redir) => err ? reply(err) : reply(redir));
|
||||
reply({url: url});
|
||||
},
|
||||
config: {
|
||||
validate: {
|
||||
@@ -53,14 +44,9 @@ module.exports = [
|
||||
method: 'GET',
|
||||
path:'/{hash}',
|
||||
handler(request, reply) {
|
||||
const query = {
|
||||
'short': `${baseUrl}/${request.params.hash}`
|
||||
};
|
||||
Shrt.findOne(query, (err, shrt) => {
|
||||
if (err) return reply(err);
|
||||
else if (shrt) reply().redirect(shrt.url);
|
||||
else reply.file('views/404.html').code(404);
|
||||
});
|
||||
const obj = model.search(`${baseUrl}/${request.params.hash}`);
|
||||
if (!obj) return reply.file('views/404.html').code(404);
|
||||
reply().redirect(obj.url);
|
||||
}
|
||||
},
|
||||
];
|
||||
|
18
shrt.js
18
shrt.js
@@ -1,8 +1,6 @@
|
||||
const Hapi = require('hapi');
|
||||
const server = new Hapi.Server();
|
||||
const routes = require('./routes');
|
||||
const mongoose = require('mongoose');
|
||||
const mongoUri = process.env.MONGOURI || 'mongodb://localhost/shortio';
|
||||
|
||||
const options = {
|
||||
server: {
|
||||
@@ -12,23 +10,17 @@ const options = {
|
||||
socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 }
|
||||
}
|
||||
};
|
||||
mongoose.connect(mongoUri, options);
|
||||
|
||||
const db = mongoose.connection;
|
||||
|
||||
server.connection({
|
||||
port: process.env.PORT || 3000,
|
||||
routes: { cors: true }
|
||||
});
|
||||
server.register(require('inert'), (err) => {
|
||||
db.on('error', console.error.bind(console, 'connection error:'))
|
||||
.once('open', () => {
|
||||
server.route(routes);
|
||||
server.route(routes);
|
||||
|
||||
server.start(err => {
|
||||
if (err) throw err;
|
||||
server.start(err => {
|
||||
if (err) throw err;
|
||||
|
||||
console.log(`Server running at port ${server.info.port}`);
|
||||
});
|
||||
});
|
||||
console.log(`Server running at port ${server.info.port}`);
|
||||
});
|
||||
});
|
||||
|
@@ -0,0 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>HTTP 404</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>HTTP 404 - Not Found</p>
|
||||
</body>
|
||||
</html>
|
||||
|
@@ -1,37 +1,64 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>shrt</title>
|
||||
<title>ve.it</title>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" href="public/style.css">
|
||||
<style>
|
||||
* {
|
||||
font-family: Sans-Serif;
|
||||
}
|
||||
#shorten-form {
|
||||
margin: auto;
|
||||
width: 500px;
|
||||
margin-top: 20vh;
|
||||
}
|
||||
input {
|
||||
border: none;
|
||||
border-bottom: 3px solid #000;
|
||||
border-left: 3px solid #000;
|
||||
padding: 5px;
|
||||
padding-bottom: 1px;
|
||||
margin: 10px;
|
||||
width: 250px;
|
||||
font-size: 16px;
|
||||
}
|
||||
#submit {
|
||||
border: none;
|
||||
padding: 5px;
|
||||
font-size: 16px;
|
||||
width: 100px;
|
||||
background: #333;
|
||||
color: #fff;
|
||||
}
|
||||
#shortened {
|
||||
width: 480px;
|
||||
margin: auto;
|
||||
font-size: 20px;
|
||||
}
|
||||
a {
|
||||
color: #000;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<main>
|
||||
<div class="container">
|
||||
<form id="shorten-form" method="POST">
|
||||
<input id="url" type="text" placeholder="URL" name="url">
|
||||
<input id="submit" type="submit" value="Shorten!">
|
||||
</form>
|
||||
<div class="row">
|
||||
<div class="column">
|
||||
<section id="shortened">
|
||||
<p>
|
||||
<em>Your shortened link: </em>
|
||||
<a id="link" href="#" target="_blank">Loading...</a>
|
||||
</p>
|
||||
<p>
|
||||
Right-click or Tap & Hold the link and copy the address to clipboard and start using it.
|
||||
</p>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
<form id="shorten-form" method="POST">
|
||||
<input id="url" type="text" placeholder="URL" name="url">
|
||||
<input id="submit" type="submit" value="Shorten!">
|
||||
</form>
|
||||
<div class="row">
|
||||
<div class="column">
|
||||
<section id="shortened">
|
||||
<p>
|
||||
<span>Shortened Link:</span>
|
||||
<a id="link" href="#" target="_blank"></a>
|
||||
</p>
|
||||
</section>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/bluebird/latest/bluebird.min.js"></script>
|
||||
<script src="https://npmcdn.com/axios/dist/axios.min.js"></script>
|
||||
<script src="public/scripts.js"></script>
|
||||
<script src="public/script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
Reference in New Issue
Block a user