Advertisement
Guest User

Untitled

a guest
Mar 31st, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. var fs = require('fs');
  2. var http = require('http');
  3. var url = require('url');
  4. var ROOT_DIR = "html/";
  5. var qs = require('querystring');
  6.  
  7. const MONGO_URL = "mongodb://localhost/mean";
  8. const MONGO_USERNAME = "matchMaker";
  9. const MONGO_PASSWORD = "p@ssw0rd";
  10.  
  11. const mongoose = require('mongoose');
  12. mongoose.connect(MONGO_URL, {
  13. auth: {
  14. user: MONGO_USERNAME,
  15. password: MONGO_PASSWORD
  16. },
  17. useNewUrlParser: true
  18. });
  19. mongoose.set('useCreateIndex', true);
  20. const meanSchema = require('./mean_schema.js').meanSchema;
  21. const User = mongoose.model('User', meanSchema);
  22.  
  23. mongoose.connection.once('open', function () {
  24. console.log("Open connection!");
  25. });
  26.  
  27. const bcrypt = require('bcrypt');
  28.  
  29. http.createServer(function (req, res) {
  30. var urlObj = url.parse(req.url, true, false);
  31. console.log(urlObj.pathname);
  32.  
  33. if (req.method == "GET") {
  34. if (urlObj.pathname.slice(urlObj.pathname.indexOf(".") + 1) == "html") {
  35. fs.readFile(ROOT_DIR + urlObj.pathname, function (err,data) {
  36. sendFile(err, data, res);
  37. });
  38. } else {
  39. fs.readFile(urlObj.pathname.slice(1), function (err,data) {
  40. sendFile(err, data, res);
  41. });
  42. }
  43. } else if (req.method == "POST" && req.url === "/index.html") {
  44. var body = '';
  45.  
  46. req.on('data', function (data) {
  47. body += data;
  48.  
  49. // Too much POST data, kill the connection!
  50. // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
  51. if (body.length > 1e6) {
  52. req.connection.destroy();
  53. mongoose.disconnect();
  54. }
  55. });
  56.  
  57. req.on('end', function () {
  58. var post = qs.parse(body);
  59. console.log(post.username);
  60. console.log(post.password);
  61. console.log(post.formValue); //trying to pass the hidden form value to determine if login or registration
  62.  
  63. /*
  64. Need to check if password is empty, because an empty
  65. password will still get hashed, so the Schema validation
  66. will see a hashed password and pass the required check.
  67. */
  68. if (!post.password || post.password.length <= 0) {
  69. res.writeHead(404);
  70. res.end("Password is required!");
  71. return;
  72. }
  73.  
  74. bcrypt.hash(post.password, 10, function(err, hash) {
  75. console.log(post.password + " is hashed to: " + hash);
  76.  
  77. var newUser = new User({
  78. userName: post.username,
  79. password: hash
  80. });
  81.  
  82.  
  83.  
  84. meanSchema.index({userName: 'post.username'});
  85.  
  86. //check request url
  87. if(req.url === "/index.html"){ //have to change between index and personalProfile until I can read form namevalues to determine which input
  88. //check if user in database
  89. User.findOne({userName: post.username}, function(err, user) {
  90. //return 412 if user doesnt exist
  91. if(user==null){
  92. console.log("user doesnt exist");
  93. res.writeHead(412);
  94. res.end(JSON.stringify(err));
  95. }else{
  96. //if user exists compare passwords
  97. bcrypt.compare(post.password, user.password, function (err, doc) {
  98. if (doc == true) {
  99. console.log("correct password");
  100. res.writeHead(302);
  101. res.end(JSON.stringify(doc));
  102. } else {
  103. console.log("incorrect password");
  104. res.writeHead(412);
  105. res.end(JSON.stringify(err));
  106. }
  107. });
  108. }
  109. });
  110. }
  111.  
  112. if(req.url === "/personalProfile.html") { //have to change between index and personalProfile until I can read form namevalues to determine which input
  113. newUser.save({}, function (err, doc) {
  114. if (err) {
  115. console.log(err);
  116. res.writeHead(412);
  117. res.end(JSON.stringify(err));
  118. } else {
  119. console.log("\nSaved document: " + doc);
  120. res.writeHead(200);
  121. res.end(JSON.stringify(doc));
  122. }
  123. // mongoose.disconnect();
  124. });
  125. }
  126. });
  127. });
  128. } else if (req.method == "POST" && req.url === "/personalProfile.html") {
  129. var body = '';
  130.  
  131. req.on('data', function (data) {
  132. body += data;
  133.  
  134. // Too much POST data, kill the connection!
  135. // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
  136. if (body.length > 1e6) {
  137. req.connection.destroy();
  138. mongoose.disconnect();
  139. }
  140. });
  141.  
  142. req.on('end', function () {
  143. var post = qs.parse(body);
  144. console.log(post.firstName);
  145. console.log(post.lastName);
  146. console.log(post.interests);
  147.  
  148. var newUser = new User({
  149. firstName: post.firstName,
  150. lastName: post.lastName,
  151. interests: post.interests,
  152. });
  153.  
  154. User.findOneAndUpdate({userName: post.username}, function (err, doc) {
  155. if (err) {
  156. console.log(err);
  157. res.writeHead(412);
  158. res.end(JSON.stringify(err));
  159. } else {
  160. console.log("\nSaved document: " + doc);
  161. res.writeHead(200);
  162. res.end(JSON.stringify(doc));
  163. }
  164.  
  165.  
  166. });
  167. })
  168. }
  169. }).listen(8080);
  170. function sendFile(err, data, res) {
  171. if (err) {
  172. res.writeHead(404);
  173. res.end(JSON.stringify(err));
  174. return;
  175. }
  176.  
  177. res.writeHead(200);
  178. res.end(data);
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement