Advertisement
Guest User

Untitled

a guest
Nov 19th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. var express = require('express');
  2. var util = require('./lib/utility');
  3. var partials = require('express-partials');
  4. var bodyParser = require('body-parser');
  5.  
  6.  
  7. var db = require('./app/config');
  8. var Users = require('./app/collections/users');
  9. var User = require('./app/models/user');
  10. var Links = require('./app/collections/links');
  11. var Link = require('./app/models/link');
  12. var Click = require('./app/models/click');
  13. var session = require('express-session');
  14.  
  15. var app = express();
  16.  
  17. app.set('views', __dirname + '/views');
  18. app.set('view engine', 'ejs');
  19. app.use(partials());
  20. // Parse JSON (uniform resource locators)
  21. app.use(bodyParser.json());
  22. // Parse forms (signup/login)
  23. app.use(bodyParser.urlencoded({ extended: true }));
  24. app.use(express.static(__dirname + '/public'));
  25. app.use(session({
  26. secret: "secret",
  27. resave: false,
  28. saveUninitialized: true
  29. }))
  30.  
  31.  
  32.  
  33. checkAuth = (req, res, next) => {
  34.  
  35. console.log('checkAuth ' + req.url);
  36.  
  37. // don't serve /secure to those not logged in
  38. // you should add to this list, for each and every secure url
  39. if (req.url === '/' && (!req.session || !req.session.authenticated)) {
  40. res.render('signup', { status: 403 });
  41. return;
  42. }
  43.  
  44. next();
  45. }
  46.  
  47.  
  48.  
  49. app.get('/', checkAuth,function(req, res) {
  50. //if else
  51. res.render('index');
  52.  
  53. });
  54.  
  55. app.get('/create', function(req, res) {
  56. res.render('index');
  57. });
  58.  
  59. app.get('/links', function(req, res) {
  60. Links.reset().fetch().then(function(links) {
  61. res.status(200).send(links.models);
  62. });
  63. });
  64.  
  65. app.post('/links', function(req, res) {
  66. var uri = req.body.url;
  67.  
  68. if (!util.isValidUrl(uri)) {
  69. console.log('Not a valid url: ', uri);
  70. return res.sendStatus(404);
  71. }
  72.  
  73. new Link({ url: uri }).fetch().then(function(found) {
  74. if (found) {
  75. res.status(200).send(found.attributes);
  76. } else {
  77. util.getUrlTitle(uri, function(err, title) {
  78. if (err) {
  79. console.log('Error reading URL heading: ', err);
  80. return res.sendStatus(404);
  81. }
  82.  
  83. Links.create({
  84. url: uri,
  85. title: title,
  86. baseUrl: req.headers.origin
  87. })
  88. .then(function(newLink) {
  89. res.status(200).send(newLink);
  90. });
  91. });
  92. }
  93. });
  94. });
  95.  
  96. /************************************************************/
  97. // Write your authentication routes here
  98. /************************************************************/
  99. app.get('/signup', function(req, res) {
  100. res.render('signup');
  101. });
  102. app.post('/signup', function(req, res) {
  103. // req.session.put('username', 'password');
  104. var username = req.body.username;
  105. var password = req.body.password;
  106. new User({username: username, password: password}).fetch().then(function(found) {
  107. if (found) {
  108. res.sendStatus(200);
  109. } else {
  110. console.log("Creating User");
  111. Users.create({
  112. username: username,
  113. password: password
  114. })
  115. .then(function(newUser) {
  116. res.render("index");
  117. });
  118. return res.status(200);
  119. }
  120.  
  121.  
  122. })
  123. console.log("session", req.session)
  124. });
  125.  
  126.  
  127. /************************************************************/
  128. // Handle the wildcard route last - if all other routes fail
  129. // assume the route is a short code and try and handle it here.
  130. // If the short-code doesn't exist, send the user to '/'
  131. /************************************************************/
  132.  
  133. app.get('/*', function(req, res) {
  134. new Link({ code: req.params[0] }).fetch().then(function(link) {
  135. if (!link) {
  136. res.redirect('/');
  137. } else {
  138. var click = new Click({
  139. linkId: link.get('id')
  140. });
  141.  
  142. click.save().then(function() {
  143. link.set('visits', link.get('visits') + 1);
  144. link.save().then(function() {
  145. return res.redirect(link.get('url'));
  146. });
  147. });
  148. }
  149. });
  150. });
  151.  
  152. console.log('Shortly is listening on 4568');
  153. app.listen(4568);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement