Advertisement
Aalid

Server.js

May 13th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 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. var id = 0;
  44.  
  45. /**** This part to handle default request url= localhost:8081 ******/
  46. app.get('/', function (req, res) {
  47. res.sendFile( __dirname + "/" + "homepage.html" );
  48. })
  49.  
  50. app.get('/homepage.html', function (req, res) {
  51. res.sendFile( __dirname + "/" + "homepage.html" );
  52. })
  53.  
  54. app.post('/login', urlencodedParser, function (req, res) {
  55.  
  56. // Get the values of the input text email & password
  57. email = req.body.email;
  58. password = req.body.password;
  59. console.log(email);
  60. console.log(password);
  61.  
  62. // Read JSON file containing the users to verify that the user is already registered and have access
  63. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
  64. // Note that err here is for handling any error occuring in opening the file
  65. if (err)
  66. {
  67. return console.error(err);
  68. }
  69. data = JSON.parse(data);
  70. var flag = 0;
  71. for (var user in data)
  72. {
  73. if(email == data[user].email)
  74. {
  75. flag = 1;
  76. authenticatedUser = user;
  77. break;
  78. }
  79. else
  80. {
  81. flag = 0;
  82. }
  83. }
  84. if(flag == 1)
  85. {
  86. if (password == data[authenticatedUser].password)
  87. {
  88. console.log("This is the tasks page");
  89. res.sendFile( __dirname + "/" + "ProjectPh1V1.html");
  90. }
  91. else
  92. {
  93. res.sendFile( __dirname + "/" + "error.html");
  94. }
  95. }
  96. // Handle invalid login by redirecting the user to the login page once again
  97. else {
  98. console.log("User Not found");
  99. res.sendFile( __dirname + "/" + "error.html");
  100. }
  101. })
  102. })
  103.  
  104. app.post('/array', function(req, res) {
  105. console.log("harby's herbyes");
  106. var data = req.data;
  107. console.log(data);
  108. arrays = data;
  109. fs.writeFile(__dirname + "/" + "user" + ".json", JSON.stringify(data), function (err) {
  110. if (err) return console.log(err);
  111. //console.log(JSON.stringify(data));
  112. });
  113. res.send('success');
  114. });
  115.  
  116. app.post('/register', urlencodedParser, function(req, res)
  117. {
  118. username = req.body.username;
  119. email = req.body.email;
  120. password = req.body.password;
  121. console.log(username);
  122. console.log(email);
  123. console.log(password);
  124.  
  125. var f = 0
  126. if (validateUsername(username))
  127. f++;
  128. if (validateEmail(email))
  129. f++;
  130. if(validatePassword(password))
  131. f++;
  132.  
  133. if (f == 3)
  134. {
  135. newUser = {
  136. "password" : password,
  137. "username" : username,
  138. "email" : email,
  139. "id" : id
  140. }
  141. var flag = 0;
  142. // Make sure this is a unique email address
  143. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
  144. if (err)
  145. return console.error(err);
  146. data = JSON.parse(data);
  147. usersCount = 0;
  148. id = 0;
  149. flag = 0;
  150. console.log('Going to check if there are duplicates');
  151. for (var user in data)
  152. {
  153. if(email == data[user].email)
  154. {
  155. flag=1;
  156. break;
  157. }
  158. usersCount++;
  159. id++;
  160. }
  161.  
  162. if (flag == 0)
  163. {
  164. console.log("ok no duplicates");
  165. // Add user to the JSON file database and show the homepage again
  166. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
  167. newUser = {
  168. "password" : password,
  169. "username" : username,
  170. "email" : email,
  171. "id" : id
  172. }
  173. console.log("read users file!");
  174. if (err)
  175. return console.error(err);
  176.  
  177. data = JSON.parse(data); // Converting it to string.
  178. data[usersCount] = newUser;
  179. console.log(data);
  180. res.sendFile( __dirname + "/" + "homepage.html" );
  181. app.get('/homepage.html', function (req, res) {
  182. res.sendFile( __dirname + "/" + "homepage.html" );
  183. })
  184. fs.writeFile(__dirname + "/" + "users.json",JSON.stringify(data), function (err) {
  185. if (err)
  186. return console.log(err);
  187. console.log(JSON.stringify(data));
  188. })
  189. });
  190. }
  191. else
  192. {
  193. console.log("Yes, duplicates");
  194. res.sendFile( __dirname + "/" + "error.html");
  195. }
  196. })
  197. }
  198. else
  199. {
  200. res.sendFile( __dirname + "/" + "error.html");
  201. }
  202. })
  203.  
  204. app.get('/ProjectPh1V1.html', function(req, res){
  205. res.sendFile( __dirname + "/" + "ProjectPh1V1.html" );
  206. })
  207.  
  208. var server = app.listen(8081, function () {
  209. var host = server.address().address
  210. var port = server.address().port
  211. console.log("App listening at http://%s:%s", host, port)
  212. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement