Advertisement
Guest User

Untitled

a guest
May 12th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. var express = require('express');
  2. var app = express();
  3. var bodyParser = require('body-parser');
  4. var fs = require("fs");
  5. // Create application/x-www-form-urlencoded parser
  6. var urlencodedParser = bodyParser.urlencoded({ extended: false })
  7. app.use(express.static('public'));
  8. app.use(bodyParser.json());
  9. var user;
  10. var id=0;
  11. var emails[];
  12.  
  13. //This is function where the Interface is sent
  14. app.get('/', function (req, res)
  15. {
  16. res.sendFile( __dirname + "/" + "userinterface.html" );
  17. })
  18.  
  19.  
  20. app.post('/register', urlencodedParser, function (req, res) {
  21.  
  22. // Prepare output in JSON format
  23. user =
  24. {
  25. "name":req.body.name,
  26. "email":req.body.email,
  27. "password":req.body.password
  28.  
  29. };
  30.  
  31. testEmail = req.body.email;
  32. console.log( testEmail);
  33. //Validation of E-mail
  34. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data)
  35. {
  36. // Note that err here is for handling any error occuring in opening the file
  37. data = JSON.parse( data );
  38. var flag = 0;
  39. for (var user in data){
  40. if(testEmail == data[user].email)
  41. {
  42. flag=1;
  43. break;
  44. }
  45. }
  46. /*
  47. //front end...........
  48. function sendArrays() {
  49. var arrays = {
  50. 'inProgressArray[]': inProgress,
  51. 'completedArray[]': completed,
  52. 'archivedArray[]': archived
  53. }
  54. $.ajax({
  55. url: "http://127.0.0.1:8081/array",
  56. type: "POST",
  57. dataType: 'json',
  58. contentType: 'application/json',
  59. data: JSON.stringify(arrays)
  60. });
  61. }
  62. //BackEnd............
  63. app.post('/array', function(req, res) {
  64. var data = req.body;
  65. arrays = data;
  66. fs.writeFile(__dirname + "/database/data/" + "user" + thisUser + ".json",JSON.stringify(data), function (err) {
  67. if (err) return console.log(err);
  68. //console.log(JSON.stringify(data));
  69. });
  70. res.send('success');
  71. });
  72. */
  73. if(flag==0)
  74. {
  75. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data)
  76. {
  77. user =
  78. {
  79. "name":req.body.name,
  80. "email":req.body.email,
  81. "password":req.body.password
  82. };
  83.  
  84. data = JSON.parse( data );
  85. data[id] = user;
  86. // counter of users starts from 0.
  87. //We make array of saved e-mails with its corresponding ids...
  88. emails.push(req.body.email);
  89. id=id+1;
  90. res.sendFile( __dirname + "/" + "finalOfPhase1.html" );
  91.  
  92. fs.writeFile(__dirname + "/" + "users.json",JSON.stringify(data), function (err) {
  93. if (err)
  94. return console.log(err);
  95. console.log(JSON.stringify(data));
  96. });
  97.  
  98. });
  99. } else {
  100. res.sendFile( __dirname + "/" + "Register.html" );
  101. }
  102. });
  103. })
  104. //BackEnd...........................................
  105. app.post('/array', function(req, res) {
  106. var data = req.body;
  107. //Creating JSON file for each user..
  108. console.log ("Server started and collections cleared.");
  109. //varialble id//
  110. fs.writeFile(__dirname + "/data/" + "user" + id + ".json",JSON.stringify(data), function (err) {
  111. if (err)
  112. return console.log(err);
  113. //console.log(JSON.stringify(data));
  114. });
  115. res.send('success');
  116. });
  117.  
  118.  
  119.  
  120. app.post('/logout', function (req, res) {
  121. res.sendFile( __dirname + "/" + "userinterface.html" );
  122. })
  123.  
  124. app.get('/register', function (req, res)
  125. {
  126. res.sendFile( __dirname + "/" + "finalOfPhase1.html" );
  127. })
  128.  
  129. app.post('/register', urlencodedParser, function (req, res) {
  130.  
  131. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data)
  132. {
  133. data = JSON.parse( data );
  134. data["i"] = user["i"];
  135. console.log( data );
  136. res.end( JSON.stringify(data));
  137.  
  138. fs.writeFile(__dirname + "/" + "users.json",JSON.stringify(data), function (err)
  139. {
  140. if (err) return console.log(err);
  141. console.log(JSON.stringify(data));
  142. });
  143.  
  144. });
  145. })
  146.  
  147. app.post('/login', urlencodedParser, function (req, res)
  148. {
  149.  
  150. // Get the values of the input text named username & password
  151. email = req.body.email1;
  152. password = req.body.password1;
  153.  
  154. console.log(email);
  155. // Read JSON file containing the users to verify that the user is already registered and have access
  156. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
  157. // Note that err here is for handling any error occuring in opening the file
  158. data = JSON.parse( data );
  159. var flag = 0;
  160. for (var user in data) {
  161. if(email == data[user].email && password==data[user].password){flag = 1; authenticatedUser = user; break;}
  162. else{flag = 0; }
  163. }
  164.  
  165. if(flag == 1)
  166. {
  167. // Handling verified users by opening their html from the json file.
  168. //fs.readFile( __dirname + "/" + "tables.json", 'utf8', function (err, tables_data) {
  169. //tables_data = JSON.parse(tables_data);
  170. //console.log(tables_data[user].html);
  171. // necessary in case multiple clicks are commited
  172. //res.setHeader('X-XSS-Protection', 0);
  173. // load the html of the user
  174. res.sendFile( __dirname + "/" + "finalOfPhase1.html" );
  175. }
  176. // Handle invalid login by redirecting the user to the login page once again
  177. else{res.sendFile( __dirname + "/" + "login.html" );}
  178. });
  179. })
  180.  
  181.  
  182.  
  183. var server = app.listen(8081, function () {
  184. var host = server.address().address
  185. var port = server.address().port
  186. console.log("Example app listening at http://%s:%s", host, port)
  187. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement