-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlshort.js
40 lines (27 loc) · 896 Bytes
/
urlshort.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
var express = require("express");
var app = express();
var mongodb = require("mongodb");
var MongoClient = mongodb.MongoClient;
var url = "mongodb://obinnaeye:" + process.env.PASS + "@ds145188.mlab.com:45188/urlshorter";
//Set the server homepage route
app.get("/", function(req, res){
res.send("Ok");
});
var host = "https://urlshortener-obinnaeye.c9users.io/";
//Set the /:url route
app.get("/:url", function(req, res){
var uri = req.params.url;
var obj = {
originalURL: host + uri,
shortURL : host + 6
};
MongoClient.connect(url, function (err, db) {
if(err) throw err;
var urls = db.collections("urls");
res.send(urls.find({originalURL: "just an example"}).toArray());
db.close()
});
});
app.listen(process.env.PORT, function(){
console.log("Server Running at: ", process.env.PORT, url);
});