-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
67 lines (62 loc) · 1.91 KB
/
server.js
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
var express = require("express");
var mongo = require('mongodb').MongoClient;
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json();
var app = express();
var mongourl = 'mongodb://localhost:27017/urls';
var favicon = require('serve-favicon');
var shortid = require('shortid');
var Q = require('q');
var config = require('./config');
var base58 = require('./base58.js');
var mongoose = require('mongoose');
var Url = require('./models/url');
var path = require('path');
//change base URL to /config.webhost/
mongoose.connect('mongodb://' + config.db.host + '/' + config.db.name);
app.use(express.static(path.join(__dirname, 'public')));
var textParser = bodyParser.text();
var baseURL = 'https://baruch-free-code-camp-baruchkogan.c9users.io/shorturl/';
app.get('/api/:url', textParser, function (req, res) {
var longUrl = req.params.url;
var shortUrl = '';
Url.findOne ({long_url : longUrl}, function(err, doc){
if (err){
console.log(err);
}
if(doc){
shortUrl = baseURL + base58.encode(doc._id);
res.send({'shortUrl' : shortUrl});
}
else{
var newUrl = Url({
long_url: longUrl
});
newUrl.save(function(err){
if(err){
console.log(err);
}
shortUrl = baseURL +base58.encode(newUrl._id);
res.send({'shortUrl': shortUrl});
});
}
}
);
});
app.get('/shorturl/:url', function (req, res) {
var base58Id = req.params.url;
var id = base58.decode(base58Id);
Url.findOne({'_id' : id}, function (err, doc){
if (err){
console.log(err);
}
if (doc){
res.redirect(doc.long_url);
}else{
res.redirect(config.webhost);
}
});
});
app.listen(8080, function () {
console.log('Microservice URL shortener is now listening on port 8080');
});