Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express');
- var util = require('./lib/utility');
- var partials = require('express-partials');
- var bodyParser = require('body-parser');
- var db = require('./app/config');
- var Users = require('./app/collections/users');
- var User = require('./app/models/user');
- var Links = require('./app/collections/links');
- var Link = require('./app/models/link');
- var Click = require('./app/models/click');
- var session = require('express-session');
- var app = express();
- app.set('views', __dirname + '/views');
- app.set('view engine', 'ejs');
- app.use(partials());
- // Parse JSON (uniform resource locators)
- app.use(bodyParser.json());
- // Parse forms (signup/login)
- app.use(bodyParser.urlencoded({ extended: true }));
- app.use(express.static(__dirname + '/public'));
- app.use(session({
- secret: "secret",
- resave: false,
- saveUninitialized: true
- }))
- checkAuth = (req, res, next) => {
- console.log('checkAuth ' + req.url);
- // don't serve /secure to those not logged in
- // you should add to this list, for each and every secure url
- if (req.url === '/' && (!req.session || !req.session.authenticated)) {
- res.render('signup', { status: 403 });
- return;
- }
- next();
- }
- app.get('/', checkAuth,function(req, res) {
- //if else
- res.render('index');
- });
- app.get('/create', function(req, res) {
- res.render('index');
- });
- app.get('/links', function(req, res) {
- Links.reset().fetch().then(function(links) {
- res.status(200).send(links.models);
- });
- });
- app.post('/links', function(req, res) {
- var uri = req.body.url;
- if (!util.isValidUrl(uri)) {
- console.log('Not a valid url: ', uri);
- return res.sendStatus(404);
- }
- new Link({ url: uri }).fetch().then(function(found) {
- if (found) {
- res.status(200).send(found.attributes);
- } else {
- util.getUrlTitle(uri, function(err, title) {
- if (err) {
- console.log('Error reading URL heading: ', err);
- return res.sendStatus(404);
- }
- Links.create({
- url: uri,
- title: title,
- baseUrl: req.headers.origin
- })
- .then(function(newLink) {
- res.status(200).send(newLink);
- });
- });
- }
- });
- });
- /************************************************************/
- // Write your authentication routes here
- /************************************************************/
- app.get('/signup', function(req, res) {
- res.render('signup');
- });
- app.post('/signup', function(req, res) {
- // req.session.put('username', 'password');
- var username = req.body.username;
- var password = req.body.password;
- new User({username: username, password: password}).fetch().then(function(found) {
- if (found) {
- res.sendStatus(200);
- } else {
- console.log("Creating User");
- Users.create({
- username: username,
- password: password
- })
- .then(function(newUser) {
- res.render("index");
- });
- return res.status(200);
- }
- })
- console.log("session", req.session)
- });
- /************************************************************/
- // Handle the wildcard route last - if all other routes fail
- // assume the route is a short code and try and handle it here.
- // If the short-code doesn't exist, send the user to '/'
- /************************************************************/
- app.get('/*', function(req, res) {
- new Link({ code: req.params[0] }).fetch().then(function(link) {
- if (!link) {
- res.redirect('/');
- } else {
- var click = new Click({
- linkId: link.get('id')
- });
- click.save().then(function() {
- link.set('visits', link.get('visits') + 1);
- link.save().then(function() {
- return res.redirect(link.get('url'));
- });
- });
- }
- });
- });
- console.log('Shortly is listening on 4568');
- app.listen(4568);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement