Advertisement
Guest User

Untitled

a guest
May 13th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1. //Importing Important libraries...
  2. var express = require('express');
  3. var app = express();
  4. var bodyParser = require('body-parser');
  5. var fs = require("fs");
  6.  
  7. // Create application/x-www-form-urlencoded parser...
  8. var urlencodedParser = bodyParser.urlencoded({ extended: false })
  9. app.use(express.static('public'));
  10. app.use(bodyParser.json());
  11.  
  12. //Important Global Variables...
  13. var user;
  14. var id=0;
  15.  
  16.  
  17. //On opening the server .. This function is called to load the User Interface...
  18. app.get('/', function (req, res)
  19. {
  20. res.sendFile( __dirname + "/" + "userinterface.html" );
  21. })
  22.  
  23. //These function validates the incoming e-mail, pw & name ^_^..
  24. function validateEmail(email)
  25. {
  26. // using regular expression.
  27. if (email.length > 0)
  28. {
  29. 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,}))$/;
  30. return re.test(email);
  31. }
  32. else
  33. return false;
  34. }
  35.  
  36. // Password validation function
  37. function validatePassword(pass)
  38. {
  39. if (pass.length < 5)
  40. return false;
  41. else
  42. return true;
  43. }
  44.  
  45. // Username validation function
  46. function validateUsername(name)
  47. {
  48. if (name.length > 0)
  49. return /^[a-zA-Z]*$/.test(name);
  50. else
  51. return false;
  52. }
  53.  
  54. //Registeration Function...
  55. app.post('/register', urlencodedParser, function (req, res) {
  56.  
  57. // Prepare output in JSON format
  58. user =
  59. {
  60. "name":req.body.name,
  61. "email":req.body.email,
  62. "password":req.body.password
  63.  
  64. };
  65.  
  66. //Validation of the Email...
  67. testEmail = req.body.email;
  68. console.log( testEmail);
  69.  
  70. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data)
  71. {
  72. //"err" here is for handling any error occuring in opening the file...
  73. data = JSON.parse( data );
  74. var flag = 0;
  75. for (var user in data){
  76. if(testEmail == data[user].email)
  77. {
  78. flag=1;
  79. break;
  80. }
  81. }
  82.  
  83. //This loop is used to determine the current id of the new user...
  84. id=0;
  85. for (var user in data){
  86. if(testEmail == data[user].email)
  87. {
  88. id=id+1;
  89. }
  90. else
  91. {
  92. id=id+1;
  93. }
  94. }
  95.  
  96.  
  97. if(flag==0)
  98. {
  99. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data)
  100. {
  101. user =
  102. {
  103. "name":req.body.name,
  104. "email":req.body.email,
  105. "password":req.body.password
  106. };
  107. if(validateEmail(user.email)) {
  108. if(validatePassword(user.password)) {
  109. if(validateUsername(user.name)) {
  110. data = JSON.parse( data );
  111. data[id] = user;
  112. //id=id+1;
  113. res.sendFile(__dirname + "/" + "finalOfPhase1.html" );
  114.  
  115. fs.writeFile(__dirname + "/" + "users.json",JSON.stringify(data), function (err) {
  116. if (err) return console.log(err);
  117. console.log(JSON.stringify(data));
  118. });
  119. } else {
  120. res.sendFile( __dirname + "/" + "Register.html" );
  121. }
  122. } else {
  123. res.sendFile( __dirname + "/" + "Register.html" );
  124. }
  125. }
  126. else {
  127. res.sendFile( __dirname + "/" + "Register.html" );
  128. }
  129. });
  130.  
  131. } else {
  132. res.sendFile( __dirname + "/" + "Register.html" );
  133. }
  134. });
  135. })
  136.  
  137.  
  138. //BackEnd...
  139. app.post('/array', function(req, res) {
  140. var data = req.body;
  141.  
  142. //Creating JSON file for each user...
  143. console.log ("Server started and collections cleared.");
  144.  
  145. //varialble id...
  146. fs.writeFile(__dirname + "/data/" + "user" + id + ".json",JSON.stringify(data), function (err) {
  147. if (err)
  148. return console.log(err);
  149. });
  150. res.send('success');
  151. });
  152.  
  153.  
  154.  
  155. app.post('/logout', function (req, res) {
  156. res.sendFile( __dirname + "/" + "userinterface.html" );
  157. })
  158.  
  159. //LOGIN FUNCTION
  160. app.post('/login', urlencodedParser, function (req, res)
  161. {
  162.  
  163. // Get the values of the input text named username & password
  164. email = req.body.email1;
  165. password = req.body.password1;
  166.  
  167. console.log(email);
  168. // Read JSON file containing the users to verify that the user is already registered and have access
  169. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
  170. // Note that err here is for handling any error occuring in opening the file
  171. data = JSON.parse( data );
  172. var important=0;
  173. var flag = 0;
  174. id=0;
  175. for (var user in data) {
  176. if(email == data[user].email && password==data[user].password){
  177. flag = 1;
  178. authenticatedUser = user;
  179. break;
  180. }
  181. else
  182. {
  183. id=id+1;
  184. important=important+1;
  185. flag = 0;
  186. }
  187. }
  188.  
  189. if(flag == 1)
  190. {
  191. res.sendFile( __dirname + "/data2/" + "test"+important+".html" );
  192. }
  193. else{res.sendFile( __dirname + "/" + "login.html" );}
  194. });
  195. })
  196.  
  197. //Importing Important Files
  198. app.get('/user1.json', function (req, res)
  199. {
  200. res.sendFile( __dirname + "/data/" + "user1.json" );
  201. })
  202.  
  203. app.get('/user2.json', function (req, res)
  204. {
  205. res.sendFile( __dirname + "/data/" + "user2.json" );
  206. })
  207.  
  208. app.get('/user3.json', function (req, res)
  209. {
  210. res.sendFile( __dirname + "/data/" + "user3.json" );
  211. })
  212.  
  213. app.get('/user4.json', function (req, res)
  214. {
  215. res.sendFile( __dirname + "/data/" + "user4.json" );
  216. })
  217.  
  218. app.get('/user5.json', function (req, res)
  219. {
  220. res.sendFile( __dirname + "/data/" + "user5.json" );
  221. })
  222.  
  223. app.get('/user6.json', function (req, res)
  224. {
  225. res.sendFile( __dirname + "/data/" + "user6.json" );
  226. })
  227.  
  228. app.get('/user7.json', function (req, res)
  229. {
  230. res.sendFile( __dirname + "/data/" + "user7.json" );
  231. })
  232.  
  233. app.get('/user8.json', function (req, res)
  234. {
  235. res.sendFile( __dirname + "/data/" + "user8.json" );
  236. })
  237.  
  238. app.get('/user9.json', function (req, res)
  239. {
  240. res.sendFile( __dirname + "/data/" + "user9.json" );
  241. })
  242.  
  243. app.get('/user10.json', function (req, res)
  244. {
  245. res.sendFile( __dirname + "/data/" + "user10.json" );
  246. })
  247.  
  248.  
  249.  
  250.  
  251. var server = app.listen(8081, function () {
  252. var host = server.address().address
  253. var port = server.address().port
  254. console.log("Example app listening at http:// %s : %s", host, port)
  255. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement