Advertisement
Aalid

Server.js

May 12th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 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. // 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. res.sendFile( __dirname + "/" + "ProjectPh1V1.html");
  88. }
  89. else
  90. {
  91. res.sendFile( __dirname + "/" + "homepage.html");
  92. }
  93. }
  94. // Handle invalid login by redirecting the user to the login page once again
  95. else {
  96. console.log("User Not found");
  97. res.sendFile( __dirname + "/" + "homepage.html");
  98. }
  99. })
  100. })
  101.  
  102. app.post('/array', function(req, res) {
  103. console.log("harby's herbyes");
  104. var data = req.body;
  105. arrays = data;
  106. fs.writeFile(__dirname + "/" + "user" + ".json", JSON.stringify(data), function (err) {
  107. if (err) return console.log(err);
  108. //console.log(JSON.stringify(data));
  109. });
  110. res.send('success');
  111. });
  112.  
  113. app.post('/register', urlencodedParser, function(req, res)
  114. {
  115. username = req.body.username;
  116. email = req.body.email;
  117. password = req.body.password;
  118. console.log(username);
  119. console.log(email);
  120. console.log(password);
  121.  
  122. var f = 0
  123. if (validateUsername(username))
  124. f++;
  125. if (validateEmail(email))
  126. f++;
  127. if(validatePassword(password))
  128. f++;
  129.  
  130. if (f == 3)
  131. {
  132. newUser = {
  133. "password" : password,
  134. "username" : username,
  135. "email" : email
  136. }
  137. var flag = 0;
  138. // Make sure this is a unique email address
  139. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
  140. if (err)
  141. return console.error(err);
  142. data = JSON.parse(data);
  143. usersCount = 0;
  144. flag = 0;
  145. console.log('Going to check if there are duplicates');
  146. for (var user in data)
  147. {
  148. if(email == data[user].email)
  149. {
  150. flag=1;
  151. break;
  152. }
  153. usersCount++;
  154. }
  155.  
  156. if (flag == 0)
  157. {
  158. console.log("ok no duplicates");
  159. // Add user to the JSON file database and show the homepage again
  160. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
  161. newUser = {
  162. "password" : password,
  163. "username" : username,
  164. "email" : email
  165. }
  166. console.log("read users file!");
  167. if (err)
  168. return console.error(err);
  169.  
  170. data = JSON.parse(data); // Converting it to string.
  171. data[usersCount] = newUser;
  172. console.log(data);
  173. res.sendFile( __dirname + "/" + "homepage.html" );
  174. app.get('/homepage.html', function (req, res) {
  175. res.sendFile( __dirname + "/" + "homepage.html" );
  176. })
  177. fs.writeFile(__dirname + "/" + "users.json",JSON.stringify(data), function (err) {
  178. if (err)
  179. return console.log(err);
  180. console.log(JSON.stringify(data));
  181. })
  182. });
  183. }
  184. else
  185. {
  186. console.log("Yes, duplicates");
  187. // Show error message and freeze modal
  188. res.sendFile( __dirname + "/" + "homepage.html");
  189. }
  190. })
  191. }
  192. else
  193. {
  194. // Show error message and freeze modal
  195. res.sendFile( __dirname + "/" + "homepage.html");
  196. }
  197. })
  198.  
  199. app.get('/ProjectPh1V1.html', function(req, res){
  200. res.sendFile( __dirname + "/" + "ProjectPh1V1.html" );
  201. })
  202.  
  203. var server = app.listen(8081, function () {
  204. var host = server.address().address
  205. var port = server.address().port
  206. console.log("App listening at http://%s:%s", host, port)
  207. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement