Skip to content

Commit 5ecaa7f

Browse files
first running version
1 parent 9f01aa0 commit 5ecaa7f

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed

run.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#! /bin/sh
2+
mongod --smallfiles & node server.js

server.js

+60-19
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
'use strict';
22

33
var express = require('express');
4-
var routes = require('./app/routes/index.js');
5-
var mongoose = require('mongoose');
6-
var passport = require('passport');
7-
var session = require('express-session');
4+
var mongoose = require('mongoose');
85
var path=require('path');
9-
6+
var fs=require('fs');
107

118
var app = express();
129
app.use('/', function (req,res,next)
@@ -18,28 +15,72 @@ app.use('/', function (req,res,next)
1815
}
1916
else // we got a time request so need to return the json
2017
{
21-
//check if date can be parsed
22-
var d=Date.parse(getpath);
23-
if (d)
18+
mongoose.connect('mongodb://localhost:27017/clementinejs');//connect to db
19+
//load all model files (with tables definition)
20+
fs.readdirSync(__dirname+'/models').forEach(function(filename){
21+
if (filename.indexOf('.js')) {
22+
//console.log(__dirname+'/models/'+filename) ;
23+
require (__dirname+'/models/'+filename);
24+
}
25+
});
26+
var reg=/(^new\/)/gi;
27+
if (!reg.test(getpath))
2428
{
25-
//console.log(d);
26-
//console.log(new Date(d).getFullYear());
27-
var jsn={"unix":d/1000,"natural":new Date(d).toDateString()};
28-
res.send(jsn);
29+
//redirect if there is no /new in the request
30+
mongoose.model('url').findOne({"shortUrlId":getpath})
31+
.exec(function(err,doc)
32+
{
33+
if (err) console.log('Err search'+err);
34+
if (doc!=null && doc!=undefined){ mongoose.connection.close();console.log('redirecting to '+ doc.originalUrl);res.redirect(doc.originalUrl);}
35+
else {mongoose.connection.close();res.end("Shortened url "+getpath+" does not exist!");}
36+
});
37+
}
38+
else
39+
{
40+
getpath=getpath.replace(/(^new\/)/gi,""); //remove leading new/
41+
var regURLvalidate= /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A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\)\*\+,;=]|:|@)|\/|\?)*)?$/i
42+
//check if URL
43+
if (regURLvalidate.test(getpath))
44+
{
45+
console.log('url '+getpath+' is good!');
46+
//check what link number we have reached and add a new entry in the database
47+
//now we have the model defined, we can access directly and find last record
48+
var findLastUrl=mongoose.model('url').findOne()
49+
.sort('-shortUrlId')
50+
.exec(function(err, doc)
51+
{
52+
if (err) console.log('Err'+err);
53+
else
54+
{
55+
if (doc==null) {var max=0;} else
56+
{
57+
var max = doc.shortUrlId;
58+
}
59+
console.log(max.toString());
60+
var ob={"shortUrlId":max+1,"originalUrl":getpath,"ip":req.headers["x-forwarded-for"]};
61+
mongoose.model('url').create(ob,function(err,data){
62+
if (err) console('Error on insert '+err);
63+
mongoose.connection.close();
64+
});
65+
66+
var jsn={"original_url":getpath,"shortened_url":req.protocol + '://' + req.get('host') +'/'+(max+1).toString()};
67+
res.send(JSON.stringify(jsn));
68+
}
69+
}
70+
);
71+
if (findLastUrl==null) {
72+
console.log('Empty Db err');
73+
}
74+
2975
}
3076
else
3177
{
32-
//check if valid unix timestamp
33-
var d=getpath*1000;
34-
var valid = (new Date(d)).getTime() > 0;
35-
if (valid)
36-
var jsn={"unix":d/1000,"natural":new Date(d).toDateString()};
37-
else
38-
var jsn={"unix":null,"natural":null};
78+
var jsn={"error":getpath+" is not a valid URL!"};
3979
res.send(jsn);
4080
//console.log(getpath);
4181
}
4282
}
83+
}
4384
});
4485

4586
var port = process.env.PORT || 8080;

0 commit comments

Comments
 (0)