Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. module.js:340
  2. throw err;
  3. ^
  4. Error: Cannot find module 'connect-mongo'
  5. at Function.Module._resolveFilename (module.js:338:15)
  6. at Function.Module._load (module.js:280:25)
  7. at Module.require (module.js:364:17)
  8. at require (module.js:380:17)
  9. at Object.<anonymous> (/home/amitnaik01/Login/server.js:6:18)
  10. at Module._compile (module.js:456:26)
  11. at Object.Module._extensions..js (module.js:474:10)
  12. at Module.load (module.js:356:32)
  13. at Function.Module._load (module.js:312:12)
  14. at Function.Module.runMain (module.js:497:10)
  15.  
  16. {
  17. "name": "ExpressApp",
  18. "version": "0.0.1",
  19. "description": "A Node.js App using express",
  20. "dependencies": {
  21. "express": "~4.0.0",
  22. "body-parser": "~1.0.2",
  23. "express-session": "~1.0.3",
  24. "express3-handlebars": "~0.5.0",
  25. "mongodb": "~1.4.2",
  26. "connect-mongo": "git+https://github.com/kcbanner/connect-mongo.git#master",
  27. "cookie-parser": "~1.0.1"
  28. },
  29. "engine": "node >=0.6.x"
  30. }
  31.  
  32. var express = require('express');
  33. var app = express();
  34. var expressSession = require('express-session');
  35. var expressHbs = require('express3-handlebars');
  36. var mongoUrl = 'mongodb://localhost:27017/dbname';
  37. var MongoStore = require('connect-mongo')(expressSession);
  38. var mongo = require('./mongo');
  39. var port = 8000; // for heroku you would use process.env.PORT instead
  40.  
  41. // This is a middleware that we will use on routes where
  42. // we _require_ that a user is logged in, such as the /secret url
  43. function requireUser(req, res, next){
  44. if (!req.user) {
  45. res.redirect('/not_allowed');
  46. } else {
  47. next();
  48. }
  49. }
  50.  
  51. // This middleware checks if the user is logged in and sets
  52. // req.user and res.locals.user appropriately if so.
  53. function checkIfLoggedIn(req, res, next){
  54. if (req.session.username) {
  55. var coll = mongo.collection('users');
  56. coll.findOne({username: req.session.username}, function(err, user){
  57. if (user) {
  58. // set a 'user' property on req
  59. // so that the 'requireUser' middleware can check if the user is
  60. // logged in
  61. req.user = user;
  62.  
  63. // Set a res.locals variable called 'user' so that it is available
  64. // to every handlebars template.
  65. res.locals.user = user;
  66. }
  67.  
  68. next();
  69. });
  70. } else {
  71. next();
  72. }
  73. }
  74.  
  75. // Use this so we can get access to `req.body` in our posted login
  76. // and signup forms.
  77. app.use( require('body-parser')() );
  78.  
  79. // We need to use cookies for sessions, so use the cookie parser middleware
  80. app.use( require('cookie-parser')() );
  81.  
  82. app.use( expressSession({
  83. secret: 'somesecretrandomstring',
  84. store: new MongoStore({
  85. url: mongoUrl
  86. })
  87. }));
  88.  
  89. // We must use this middleware _after_ the expressSession middleware,
  90. // because checkIfLoggedIn checks the `req.session.username` value,
  91. // which will not be available until after the session middleware runs.
  92. app.use(checkIfLoggedIn);
  93.  
  94. app.engine('hbs', expressHbs({extname:'hbs', defaultLayout:'main.hbs'}));
  95. app.set('view engine', 'hbs');
  96.  
  97. app.get('/', function(req, res){
  98. var coll = mongo.collection('users');
  99. coll.find({}).toArray(function(err, users){
  100. res.render('index', {users:users});
  101. })
  102. });
  103.  
  104. app.get('/login', function(req, res){
  105. res.render('login');
  106. });
  107.  
  108. app.get('/logout', function(req, res){
  109. delete req.session.username;
  110. res.redirect('/');
  111. });
  112.  
  113. app.get('/not_allowed', function(req, res){
  114. res.render('not_allowed');
  115. });
  116.  
  117. // The /secret url includes the requireUser middleware.
  118. app.get('/secret', requireUser, function(req, res){
  119. res.render('secret');
  120. });
  121.  
  122. app.get('/signup', function(req,res){
  123. res.render('signup');
  124. });
  125.  
  126. // This creates a new user and calls the callback with
  127. // two arguments: err, if there was an error, and the created user
  128. // if a new user was created.
  129. //
  130. // Possible errors: the passwords are not the same, and a user
  131. // with that username already exists.
  132. function createUser(username, password, password_confirmation, callback){
  133. var coll = mongo.collection('users');
  134.  
  135. if (password !== password_confirmation) {
  136. var err = 'The passwords do not match';
  137. callback(err);
  138. } else {
  139. var query = {username:username};
  140. var userObject = {username: username, password: password};
  141.  
  142. // make sure this username does not exist already
  143. coll.findOne(query, function(err, user){
  144. if (user) {
  145. err = 'The username you entered already exists';
  146. callback(err);
  147. } else {
  148. // create the new user
  149. coll.insert(userObject, function(err,user){
  150. callback(err,user);
  151. });
  152. }
  153. });
  154. }
  155. }
  156.  
  157. app.post('/signup', function(req, res){
  158. // The 3 variables below all come from the form
  159. // in views/signup.hbs
  160. var username = req.body.username;
  161. var password = req.body.password;
  162. var password_confirmation = req.body.password_confirmation;
  163.  
  164. createUser(username, password, password_confirmation, function(err, user){
  165. if (err) {
  166. res.render('signup', {error: err});
  167. } else {
  168.  
  169. // This way subsequent requests will know the user is logged in.
  170. req.session.username = user.username;
  171.  
  172. res.redirect('/');
  173. }
  174. });
  175. });
  176.  
  177. // This finds a user matching the username and password that
  178. // were given.
  179. function authenticateUser(username, password, callback){
  180. var coll = mongo.collection('users');
  181.  
  182. coll.findOne({username: username, password:password}, function(err, user){
  183. callback(err, user);
  184. });
  185. }
  186.  
  187. app.post('/login', function(req, res){
  188. // These two variables come from the form on
  189. // the views/login.hbs page
  190. var username = req.body.username;
  191. var password = req.body.password;
  192.  
  193. authenticateUser(username, password, function(err, user){
  194. if (user) {
  195. // This way subsequent requests will know the user is logged in.
  196. req.session.username = user.username;
  197.  
  198. res.redirect('/');
  199. } else {
  200. res.render('login', {badCredentials: true});
  201. }
  202. });
  203. });
  204.  
  205. app.use('/public', express.static('public'));
  206.  
  207. mongo.connect(mongoUrl, function(){
  208. console.log('Connected to mongo at: ' + mongoUrl);
  209. app.listen(port, function(){
  210. console.log('Server is listening on port: '+port);
  211. });
  212. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement