Skip to content

Commit 5a5fc6f

Browse files
committed
some updates
1 parent 95f71ef commit 5a5fc6f

File tree

6 files changed

+23
-52
lines changed

6 files changed

+23
-52
lines changed

app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ app.get('/', function(req, res) {
5050
})
5151

5252

53-
app.listen(3000, function() {
53+
app.listen(process.env.port || 3000, function() {
5454
console.log('Server started on localhost:3000');
5555
});

controllers/url-shortener.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@ var validUrl = require('valid-url');
33

44
module.exports = function(app) {
55
// generating a random number for shorter URL
6-
var randomNum = Math.floor(Math.random() * 10000) + 1;
6+
var randomNum;
77
// getting source url of the host, just defining as global
88
var sourceUrl;
99
// getting url that is passed as parameter, just defining as global
1010
var url;
1111

1212
// route for index.html
1313
app.get('/shortenURL', function(req, res) {
14+
15+
randomNum = Math.floor(Math.random() * 10000) + 1;
16+
1417
// getting the sourceUrl from main route and storing it globally
15-
sourceUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
16-
res.render('shortenURL-index');
18+
sourceUrl = req.protocol + '://' + req.get('host');
19+
res.render('shortenURL-index', {url: sourceUrl, num: randomNum});
1720
});
1821

1922
// route for recieving parameter for url
@@ -24,17 +27,19 @@ module.exports = function(app) {
2427
if(validUrl.isUri(url)) {
2528
res.json({
2629
original_url: req.params.url,
27-
short_url: sourceUrl + '' + randomNum
30+
short_url: sourceUrl + '/shorten/' + randomNum
2831
});
2932
// otherwise display error message
30-
} else {
31-
res.render('shortenURL-error');
3233
}
3334
});
3435

36+
3537
// route for shorter url that will redirect to the wanted url
36-
app.get('/' + randomNum, function(req, res) {
37-
res.redirect(302, url);
38+
app.get('/shorten/:num' , function(req, res) {
39+
if(req.params.num == randomNum) {
40+
res.redirect(301, url);
41+
} else {
42+
res.render('shortenURL-index', {url: sourceUrl, num: randomNum});
43+
}
3844
});
39-
4045
}

controllers/whoami.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
var os = require('os');
2-
1+
var useragent = require('express-useragent');
32
module.exports = function(app) {
43

4+
app.use(useragent.express());
5+
56
app.get('/whoami', function(req, res) {
67
res.render('whoami');
78
})
@@ -10,7 +11,7 @@ module.exports = function(app) {
1011
res.json({
1112
ipaddress: req.ip,
1213
language: req.headers["accept-language"].split(',')[0],
13-
software: `${os.type()} ${os.release()}, ${os.platform()}, ${os.arch()}`
14+
software: `${req.useragent.os} ${req.useragent.platform}, ${req.useragent.browser} ${req.useragent.version}`
1415
});
1516
});
1617
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"cors": "^2.8.3",
1414
"ejs": "^2.5.6",
1515
"express": "^4.15.3",
16+
"express-useragent": "^1.0.7",
1617
"mongoose": "^4.10.0",
1718
"multer": "^1.3.0",
1819
"node-bing-api": "^3.2.1",

views/shortenURL-error.ejs

-36
This file was deleted.

views/shortenURL-index.ejs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
</ul>
2020

2121
<h2>Example creation usage:</h2>
22-
<p>As natural: <code>https://little-url.herokuapp.com/new/https://www.google.com</code></p>
22+
<p>As natural: <code><%= url %>/shortenURL/https://www.google.com</code></p>
2323

2424
<h2>Example creation output:</h2>
25-
<code>{ "original_url":"https://www.google.com", "short_url":"https://little-url.herokuapp.com/2871" }
25+
<code>{ "original_url":"https://www.google.com", "short_url":"<%= url %>/shorten/<%= num %>" }
2626
</code>
2727
<h2>Usage:</h2>
28-
<code>https://little-url.herokuapp.com/2871</code>
28+
<code><%= url %>/shorten/2871</code>
2929

3030
<h2>Will redirect to:</h2>
3131
<code>https://www.google.com/</code>

0 commit comments

Comments
 (0)