-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
89 lines (84 loc) · 3.8 KB
/
Copy pathserver.js
File metadata and controls
89 lines (84 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use strict';
var express = require('express');
var mongoose = require('mongoose');
var path=require('path');
var fs=require('fs');
var app = express();
app.use('/', function (req,res,next)
{
var getpath=decodeURIComponent(req.path).replace(/(^\/)/g, "");//remove leading / and decode URI
if (getpath=='') //by default return the help page
{
res.sendFile(path.join(__dirname,'public/indexURLShort.html'));
}
else // we got a time request so need to return the json
{
mongoose.connect('mongodb://localhost:27017/clementinejs');//connect to db
//load all model files (with tables definition)
fs.readdirSync(__dirname+'/models').forEach(function(filename){
if (filename.indexOf('.js')) {
//console.log(__dirname+'/models/'+filename) ;
require (__dirname+'/models/'+filename);
}
});
var reg=/(^new\/)/gi;
if (!reg.test(getpath))
{
//redirect if there is no /new in the request
mongoose.model('url').findOne({"shortUrlId":getpath})
.exec(function(err,doc)
{
if (err) console.log('Err search'+err);
if (doc!=null && doc!=undefined){ mongoose.connection.close();console.log('redirecting to '+ doc.originalUrl);res.redirect(doc.originalUrl);}
else {mongoose.connection.close();res.end("Shortened url "+getpath+" does not exist!");}
});
}
else
{
getpath=getpath.replace(/(^new\/)/gi,""); //remove leading new/
var regURLvalidate= /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i
//check if URL
if (regURLvalidate.test(getpath))
{
console.log('url '+getpath+' is good!');
//check what link number we have reached and add a new entry in the database
//now we have the model defined, we can access directly and find last record
var findLastUrl=mongoose.model('url').findOne()
.sort('-shortUrlId')
.exec(function(err, doc)
{
if (err) console.log('Err'+err);
else
{
if (doc==null) {var max=0;} else
{
var max = doc.shortUrlId;
}
console.log(max.toString());
var ob={"shortUrlId":max+1,"originalUrl":getpath,"ip":req.headers["x-forwarded-for"]};
mongoose.model('url').create(ob,function(err,data){
if (err) console('Error on insert '+err);
mongoose.connection.close();
});
var jsn={"original_url":getpath,"shortened_url":req.protocol + '://' + req.get('host') +'/'+(max+1).toString()};
res.send(JSON.stringify(jsn));
}
}
);
if (findLastUrl==null) {
console.log('Empty Db err');
}
}
else
{
var jsn={"error":getpath+" is not a valid URL!"};
res.send(jsn);
//console.log(getpath);
}
}
}
});
var port = process.env.PORT || 8080;
app.listen(port, function () {
console.log('Node.js listening on port ' + port + '...');
});