Advertisement
Guest User

Untitled

a guest
May 12th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. var app = express();
  3. var fs = require("fs");
  4. var bodyParser = require('body-parser');
  5. app.use(bodyParser.json());
  6. // Create application/x-www-form-urlencoded parser
  7. var urlencodedParser = bodyParser.urlencoded({ extended: false })
  8. var authenticatedUser = null;
  9. app.use(express.static('Front End'));
  10. var arrays;
  11. // Email validation funciton
  12. function validateEmail(email)
  13. {
  14.     // using regular expression.
  15.     if (email.length > 0)
  16.     {
  17.         var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  18.         return re.test(email);
  19.     }
  20.     else
  21.         return false;
  22. }
  23.  
  24. // Password validation function
  25. function validatePassword(pass)
  26. {
  27.     if (pass.length < 5)
  28.         return false;
  29.     else
  30.         return true;
  31. }
  32.  
  33. // Username validation function
  34. function validateUsername(name)
  35. {
  36.     if (name.length > 0)
  37.         return /^[a-zA-Z]*$/.test(name);
  38.     else
  39.         return false;
  40. }
  41. // Number of users already registered
  42. var usersCount = 0;
  43.  
  44. /**** This part to handle default request url= localhost:8081 ******/
  45. app.get('/', function (req, res) {
  46.   res.sendFile( __dirname + "/" + "homepage.html" );
  47. })
  48.  
  49. app.get('/homepage.html', function (req, res) {
  50.   res.sendFile( __dirname + "/" + "homepage.html" );
  51. })
  52.  
  53. app.post('/login', urlencodedParser, function (req, res) {
  54.  
  55.   // Get the values of the input text email & password
  56.   email = req.body.email;
  57.   password = req.body.password;
  58.   console.log(email);
  59.   console.log(password);
  60.  
  61.   // Read JSON file containing the users to verify that the user is already registered and have access
  62.   fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
  63.     // Note that err here is for handling any error occuring in opening the file
  64.     if (err)
  65.     {
  66.        return console.error(err);
  67.     }      
  68.     data = JSON.parse(data);
  69.     var flag = 0;
  70.     for (var user in data)
  71.     {
  72.       if(email == data[user].email)
  73.       {
  74.         flag = 1;
  75.         authenticatedUser = user;
  76.         break;
  77.       }
  78.       else
  79.       {
  80.         flag = 0;
  81.       }
  82.     }
  83.     if(flag == 1)
  84.     {
  85.         if (password == data[authenticatedUser].password)
  86.         {
  87.             console.log("This is the tasks page");
  88.             res.sendFile( __dirname + "/" + "ProjectPh1V1.html");  
  89.         }
  90.         else
  91.         {
  92.             res.sendFile( __dirname + "/" + "homepage.html");
  93.         }
  94.     }
  95.     // Handle invalid login by redirecting the user to the login page once again
  96.     else {
  97.         console.log("User Not found");
  98.         res.sendFile( __dirname + "/" + "homepage.html");
  99.     }
  100.   })
  101.  })
  102.  
  103. app.post('/array', function(req, res) {
  104.     console.log("harby's herbyes");
  105.     var data = req.body;
  106.     console.log(data);
  107.     arrays = data;
  108.     fs.writeFile(__dirname + "/" + "user.json", JSON.stringify(data), function (err) {
  109.         if (err) return console.log(err);
  110.         //console.log(JSON.stringify(data));
  111.     });  
  112.     res.send('success');
  113. });
  114.  
  115.  app.post('/register', urlencodedParser, function(req, res)
  116.  {
  117.     username = req.body.username;
  118.     email = req.body.email;
  119.     password = req.body.password;
  120.     console.log(username);
  121.     console.log(email);
  122.     console.log(password);
  123.  
  124.     var f = 0
  125.     if (validateUsername(username))
  126.         f++;
  127.     if (validateEmail(email))
  128.         f++;
  129.     if(validatePassword(password))
  130.         f++;
  131.  
  132.     if (f == 3)
  133.     {
  134.         newUser = {
  135.             "password" : password,
  136.             "username" : username,
  137.             "email" : email
  138.         }
  139.         var flag = 0;
  140.         // Make sure this is a unique email address
  141.         fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
  142.             if (err)
  143.                 return console.error(err);
  144.             data = JSON.parse(data);
  145.             usersCount = 0;
  146.             flag = 0;
  147.             console.log('Going to check if there are duplicates');
  148.             for (var user in data)
  149.             {
  150.                 if(email == data[user].email)
  151.                 {
  152.                     flag=1;
  153.                     break;
  154.                 }
  155.                 usersCount++;
  156.             }
  157.        
  158.             if (flag == 0)
  159.             {
  160.                 console.log("ok no duplicates");
  161.                 // Add user to the JSON file database and show the homepage again  
  162.                 fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
  163.                     newUser = {
  164.                         "password" : password,
  165.                         "username" : username,
  166.                         "email" : email
  167.                     }
  168.                     console.log("read users file!");
  169.                     if (err)
  170.                         return console.error(err);
  171.                    
  172.                    data = JSON.parse(data); // Converting it to string.
  173.                    data[usersCount] = newUser;
  174.                    console.log(data);
  175.                    res.sendFile( __dirname + "/" + "homepage.html" );
  176.                    app.get('/homepage.html', function (req, res) {
  177.                         res.sendFile( __dirname + "/" + "homepage.html" );
  178.                     })
  179.                    fs.writeFile(__dirname + "/" + "users.json",JSON.stringify(data), function (err) {
  180.                        if (err)
  181.                            return console.log(err);
  182.                        console.log(JSON.stringify(data));
  183.                    })
  184.                 });
  185.             }
  186.             else
  187.             {
  188.                 console.log("Yes, duplicates");
  189.                 // Show error message and freeze modal
  190.                 res.sendFile( __dirname + "/" + "homepage.html");
  191.             }
  192.         })
  193.     }
  194.     else
  195.     {
  196.         // Show error message and freeze modal
  197.         res.sendFile( __dirname + "/" + "error.html"); 
  198.     }
  199.  })
  200.  
  201. app.get('/ProjectPh1V1.html', function(req, res){
  202.     res.sendFile( __dirname + "/" + "ProjectPh1V1.html" );
  203. })
  204.  
  205. var server = app.listen(8081, function () {
  206.     var host = server.address().address
  207.     var port = server.address().port
  208.     console.log("App listening at http://%s:%s", host, port)
  209. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement