Skip to content

Instantly share code, notes, and snippets.

@JosephLivengood
Last active August 27, 2020 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save JosephLivengood/8a335d1a68ed9170da02bb9d8f5b71d5 to your computer and use it in GitHub Desktop.
Save JosephLivengood/8a335d1a68ed9170da02bb9d8f5b71d5 to your computer and use it in GitHub Desktop.
FCC Advanced Node and Express Checkpoint 3
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const session = require('express-session');
const passport = require('passport');
const mongo = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID;
const LocalStrategy = require('passport-local');
const app = express();
fccTesting(app); //For FCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'pug')
app.use(session({
secret: process.env.SESSION_SECRET,
resave: true,
saveUninitialized: true,
}));
app.use(passport.initialize());
app.use(passport.session());
mongo.connect(process.env.DATABASE, (err, db) => {
if(err) {
console.log('Database error: ' + err);
} else {
console.log('Successful database connection');
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser( (id, done) => {
db.collection('users').findOne(
{_id: new ObjectID(id)},
(err, doc) => {
done(null, doc);
}
);
});
passport.use(new LocalStrategy(
function(username, password, done) {
db.collection('users').findOne({ username: username }, function (err, user) {
console.log('User '+ username +' attempted to log in.');
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (password !== user.password) { return done(null, false); }
return done(null, user);
});
}
));
app.route('/')
.get((req, res) => {
res.render(process.cwd() + '/views/pug/index', {title: 'Hello', message: 'login', showLogin: true});
});
app.route('/login')
.post(passport.authenticate('local', { failureRedirect: '/' }),(req,res) => {
res.redirect('/profile');
});
app.route('/profile')
.get((req,res) => {
res.render(process.cwd() + '/views/pug/profile');
});
app.listen(process.env.PORT || 3000, () => {
console.log("Listening on port " + process.env.PORT);
});
}});
@padunk
Copy link

padunk commented Jan 3, 2019

I have db.collection is not a function. So I search MongoClient.connect and since version 3.0 we now get client object containing database object instead. This code should be revised like:

mongo.connect(process.env.DATABASE, (err, client) => {
  var db = client.db('myproject');
  // code below doesn't need any change.
})

@IzzieKi
Copy link

IzzieKi commented Mar 13, 2019

I have db.collection is not a function. So I search MongoClient.connect and since version 3.0 we now get client object containing database object instead. This code should be revised like:

mongo.connect(process.env.DATABASE, (err, client) => {
  var db = client.db('myproject');
  // code below doesn't need any change.
})

if we're using strict, shouldn't it be 'let db' rather than 'var db'?

@afozbek
Copy link

afozbek commented Jun 17, 2019

I have db.collection is not a function. So I search MongoClient.connect and since version 3.0 we now get client object containing database object instead. This code should be revised like:

mongo.connect(process.env.DATABASE, (err, client) => {
  var db = client.db('myproject');
  // code below doesn't need any change.
})

if we're using strict, shouldn't it be 'let db' rather than 'var db'?

Both will work I use let in challenge. And thank you for the answer

@pmayur
Copy link

pmayur commented Jul 24, 2019

I have db.collection is not a function. So I search MongoClient.connect and since version 3.0 we now get client object containing database object instead. This code should be revised like:

mongo.connect(process.env.DATABASE, (err, client) => {
  var db = client.db('myproject');
  // code below doesn't need any change.
})

well the same error is also resolved if you require just mongodb and set it to variable db as follows
const db = require('mongodb');
the earlier require exercises pass even if you don't require it in your code, however we get an error later on

@vennelatadi
Copy link

SESSION_SECRET: command not found

@kingdopamine
Copy link

SESSION_SECRET: command not found

Just set the value of secret in app.use(session({secret...}))

@danielpyon
Copy link

Just set the value of secret in app.use(session({secret...}))

You can also just set SESSION_SECRET in .env

@veritem
Copy link

veritem commented Aug 5, 2020

I have db.collection is not a function

I encountered the same thing. In package.json, change mongodb line to "mongodb": "^2.2.33". You will need to npm uninstall mongodb; then npm install to install this version.

@kodefilter
Copy link

NOTE: v3.x was recently released with breaking API changes.

Add code to connect to the server and the database myproject:

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
 
// Connection URL
const url = 'mongodb://localhost:27017';
 
// Database Name
const dbName = 'myproject';
 
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
  assert.equal(null, err);
  console.log("Connected successfully to server");
 
  const db = client.db(dbName);
 
  client.close();
});

Read more at https://www.npmjs.com/package/mongodb

@bigclown
Copy link

I do not understand why FCC took the approach of using mongodb instead mongoose.

There is a major problem with mongoose or this was a personal choice of @JosephLivengood ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment