SHARE
TWEET

Untitled

a guest May 5th, 2016 145 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. var router = express.Router();
  3.  
  4. var mongodb = require('mongodb');
  5. var mLab = "mongodb://localhost:27017/url-shortener-microservice";
  6. var MongoClient = mongodb.MongoClient
  7.  
  8. var shortid = require('shortid');
  9. var validUrl = require('valid-url');
  10.  
  11. /* GET home page. */
  12. router.get('/', function(req, res, next) {
  13.   res.render('index', { title: 'Express' });
  14. });
  15.  
  16. router.get('/new/:url(*)', function (req, res, next) {
  17.   MongoClient.connect(mLab, function (err, db) {
  18.     if (err) {
  19.       console.log("Unable to connect to server", err);
  20.     } else {
  21.       console.log("Connected to server")
  22.      
  23.       var collection = db.collection('links');
  24.       var params = req.params.url;
  25.      
  26.       var newLink = function (db, callback) {
  27.         collection.findOne({ "url": params }, { short: 1, _id: 0 }, function (err, doc) {
  28.           if (doc != null) {
  29.             res.json({ original_url: params, short_url: "localhost:3000/" + doc.short });
  30.           } else {
  31.             if (validUrl.isUri(params)) {
  32.               // if URL is valid, do this
  33.               var shortCode = shortid.generate();
  34.               var newUrl = { url: params, short: shortCode };
  35.               collection.insert([newUrl]);
  36.               res.json({ original_url: params, short_url: "localhost:3000/" + shortCode });
  37.             } else {
  38.             // if URL is invalid, do this
  39.               res.json({ error: "Wrong url format, make sure you have a valid protocol and real site." });
  40.             };
  41.           };
  42.         });
  43.       };
  44.      
  45.       newLink(db, function () {
  46.         db.close();
  47.       });
  48.      
  49.     };
  50.   });
  51.  
  52. });
  53.  
  54. router.get('/:short', function (req, res, next) {
  55.  
  56.   MongoClient.connect(mLab, function (err, db) {
  57.     if (err) {
  58.       console.log("Unable to connect to server", err);
  59.     } else {
  60.       console.log("Connected to server")
  61.  
  62.       var collection = db.collection('links');
  63.       var params = req.params.short;
  64.  
  65.       var findLink = function (db, callback) {
  66.         collection.findOne({ "short": params }, { url: 1, _id: 0 }, function (err, doc) {
  67.           if (doc != null) {
  68.             res.redirect(doc.url);
  69.           } else {
  70.             res.json({ error: "No corresponding shortlink found in the database." });
  71.           };
  72.         });
  73.       };
  74.  
  75.       findLink(db, function () {
  76.         db.close();
  77.       });
  78.  
  79.     };
  80.   });
  81. });
  82.  
  83. module.exports = router;
RAW Paste Data
Top