/
server.js
125 lines (102 loc) · 2.96 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict';
var express = require('express');
var mongo = require('mongodb');
try{
var mongoose = require('mongoose');
} catch (e) {
console.log(e);
}
var cors = require('cors');
var dns = require('dns');
var app = express();
// Basic Configuration
var port = process.env.PORT || 3000;
/** this project needs a db !! **/
// mongoose.connect(process.env.MONGOLAB_URI);
mongoose.connect(process.env.MONGO_URI, {useNewUrlParser:true});
var schema = new mongoose.Schema({
url : {
type: String,
required: true
},
code: Number
});
var Shortlink = mongoose.model('Shortlink', schema);
app.use(cors());
/** this project needs to parse POST bodies **/
// you should mount the body-parser here
var bodyParser=require('body-parser');
app.use('/public', express.static(process.cwd() + '/public'));
app.get('/', function(req, res){
res.sendFile(process.cwd() + '/views/index.html');
});
app.use("/api/shorturl/new", bodyParser.urlencoded({extended:false}));
// your first API endpoint...
app.get("/api/hello", function (req, res) {
res.json({greeting: 'hello API'});
});
app.use("/", (req, res, next)=>{
console.log(req.method + " " + req.path + " - " + req.ip);
next();
});
app.use("/", express.static(__dirname + "/public"));
let numberOfEntries;
var updateCount = function(done){
Shortlink.find().estimatedDocumentCount((error, count)=>{
if(error) return console.log(error);
numberOfEntries = count;
done();
});
};
app.route("/api/shorturl/new").get((req, res)=>{
res.json({"hi": req.query});
}).post((req, res)=>{
dns.lookup(req.body.url, (err, add, fam)=>{
if(err){
res.json({"error":"Invalid URL"});
}else{
updateCount(()=>{
let document = new Shortlink({
url: req.body.url,
code: numberOfEntries+1
});
document.save((err, data)=>{
if(err) return "ERRR";
})
res.json({"original_url": req.body.url, "short_url":+numberOfEntries+1});
});
}
});
});
var findSite = function(codeNumber, done){
Shortlink.findOne({code: codeNumber}, (err, data)=>{
if(err) console.log("ERROROROROROROR");
done(err, data);
});
}
app.get("/api/shorturl/:code", (req, res)=>{
console.log("received " + req.params.code);
findSite(req.params.code, (err, data)=>{
if(err) return err;
res.redirect(data.url);
})
});
/*
I can POST a URL to
[project_url]/api/shorturl/new and I will receive
a shortened URL in the JSON response. Example :
{"original_url":"www.google.com",
"short_url":1}
If I pass an invalid URL that doesn't follow the
valid http(s)://www.example.com(/more/routes)
format, the JSON response will contain an error
like {"error":"invalid URL"}. HINT: to be sure
that the submitted url points to a valid site you
can use the function dns.lookup(host, cb) from the
dns core module.
When I visit the shortened URL, it will redirect me
to my original link.
*/
app.listen(port, function () {
console.log('Node.js listening ...');
});