Advertisement
Aalid

Server.js

May 12th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. var express = require('express');
  2. var app = express();
  3. var fs = require("fs");
  4. var bodyParser = require('body-parser');
  5.  
  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.  
  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.  
  42. /**** This part to handle default request url= localhost:8081 ******/
  43. app.get('/', function (req, res) {
  44. res.sendFile( __dirname + "/" + "homepage.html" );
  45. })
  46.  
  47. app.get('/homepage.html', function (req, res) {
  48. res.sendFile( __dirname + "/" + "homepage.html" );
  49. })
  50.  
  51. app.post('/login', urlencodedParser, function (req, res) {
  52.  
  53. // Get the values of the input text email & password
  54. email = req.body.email;
  55. password = req.body.password;
  56. console.log(email);
  57. console.log(password);
  58.  
  59. // Read JSON file containing the users to verify that the user is already registered and have access
  60. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
  61. // Note that err here is for handling any error occuring in opening the file
  62. if (err)
  63. {
  64. return console.error(err);
  65. }
  66. data = JSON.parse(data);
  67. var flag = 0;
  68. for (var user in data)
  69. {
  70. if(email == data[user].email)
  71. {
  72. flag = 1;
  73. authenticatedUser = user;
  74. break;
  75. }
  76. else
  77. {
  78. flag = 0;
  79. }
  80. }
  81. if(flag == 1)
  82. {
  83. if (password == data[authenticatedUser].password)
  84. {
  85. res.sendFile( __dirname + "/" + "ProjectPh1V1.html");
  86. }
  87. else
  88. {
  89. res.sendFile( __dirname + "/" + "homepage.html");
  90. }
  91. }
  92. // Handle invalid login by redirecting the user to the login page once again
  93. else {
  94. res.sendFile( __dirname + "/" + "homepage.html");
  95. }
  96. });
  97. })
  98.  
  99. app.post('/register', urlencodedParser, function(req, res)
  100. {
  101. username = req.body.username;
  102. email = req.body.email;
  103. password = req.body.password;
  104. console.log(username);
  105. console.log(email);
  106. console.log(password);
  107.  
  108. var f = 0
  109. if (validateUsername(username))
  110. f++;
  111. if (validateEmail(email))
  112. f++;
  113. if(validatePassword(password))
  114. f++;
  115.  
  116. if (f == 3)
  117. {
  118. // Add user to the JSON file database and show the homepage again
  119. console.log('Invalid user registration input');
  120. res.sendFile( __dirname + "/" + "homepage.html");
  121. }
  122. else
  123. {
  124. // Show error message and FREEZE MODEL THIS IS NOT DONE YET <----------------------------
  125. console.log('Invalid user registration input');
  126. res.sendFile( __dirname + "/" + "homepage.html");
  127. }
  128. })
  129.  
  130. app.get('/ProjectPh1V1.html', function(req, res){
  131. res.sendFile( __dirname + "/" + "ProjectPh1V1.html" );
  132. })
  133.  
  134. var server = app.listen(8081, function () {
  135. var host = server.address().address
  136. var port = server.address().port
  137. console.log("App listening at http://%s:%s", host, port)
  138. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement