Advertisement
Guest User

Untitled

a guest
Jul 5th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. //index.js file
  2.  
  3. var express = require('express');
  4. var router = express.Router();
  5.  
  6. var app = express();
  7.  
  8. var functions = require('../public/javascripts/functions.js');
  9.  
  10. router.post('/ajax', function(req, res , next){
  11.  
  12. var username = req.param("username");
  13. var password = req.param("password");
  14. var operation = req.param("operation");
  15.  
  16. else if (operation === "validate")
  17. {
  18.  
  19. functions.validate(username, password, req);
  20.  
  21. //req.session.username = "yaryar"; >>>THIS DOES WORK<<<
  22.  
  23. }
  24.  
  25. var strings = ["rad", "bla", "ska"]
  26.  
  27. console.log('body: ' + JSON.stringify(req.body));
  28. console.log("AJAX RECEIVED");
  29. res.send(strings);
  30. });
  31.  
  32. module.exports = router;
  33.  
  34. module.exports = {
  35.  
  36. validate: function(username, password, req) {
  37.  
  38. var url = 'mongodb://localhost';
  39. var MongoClient = require('mongodb').MongoClient;
  40. var assert = require('assert');
  41. var ObjectId = require('mongodb').ObjectID;
  42.  
  43. MongoClient.connect(url, function(err, db)
  44. {
  45. assert.equal(null, err);
  46. console.log("Connected correctly to server.");
  47.  
  48. var cursor = db.collection('users').find({username : username});
  49.  
  50. cursor.each(function(err,doc,req)
  51. {
  52.  
  53. assert.equal(err, null);
  54.  
  55. if (doc != null)
  56. {
  57. console.log("user found: " + doc.username);
  58.  
  59. req.session.username = "ttyy"; // >>>DOESNT WORK<<<
  60. return true
  61.  
  62. }
  63. else
  64. {
  65. console.log("user not found");
  66. return false;
  67. }
  68. });
  69. //db.close();
  70. });
  71. }
  72. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement