Advanced Node and Express - Logging a User Out Please Help me

'use strict';

const express     = require('express');
const bodyParser  = require('body-parser');
const fccTesting  = require('./freeCodeCamp/fcctesting.js');
const passport = require('passport');
const session = require('express-session');
const ObjectID = require('mongodb').ObjectID;
const mongo = require('mongodb').MongoClient;
const LocalStrategy =require('passport-local');

const app = express();

  
  
app.set('view engine', 'pug');
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.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');

        //serialization and app.listen

}});
passport.serializeUser((user, done) => {
   done(null, user._id);
 });
passport.deserializeUser((id, done) => {
        mongo.collection('users').findOne(
            {_id: new ObjectID(id)},
            (err, doc) => {
                done(null, doc);
  
            }
       );
    });
// Authentication Strategies
passport.use(new LocalStrategy(
  function(username, password, done) {
    mongo.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.pug',{title: 'Hello', message: 'Please login',showLogin: true});
  });

 app.route('/login')
          .post(passport.authenticate('local', { failureRedirect: '/' }),(req,res) => {
               res.redirect('/profile');
          });

function ensureAuthenticated(req,res, next){
if (req.isAuthenticated()){
return next();}
res.redirect('/');
}
  
        app.route('/profile')
          .get(ensureAuthenticated, (req,res) => {
               res.render(process.cwd() + '/views/pug/profile' , {username: req.user.username});
          });

app.route('/logout')
  .get((req, res) => {
      req.logout();
      res.redirect('/');
  });
app.use((req, res, next) => {
  res.status(404)
    .type('text')
    .send('Not Found');
});
  
app.listen(process.env.PORT || 3000, () => {
  console.log("Listening on port " + process.env.PORT);
});
 

What is the problem with my code

Please some help me to pass this challenge !!

Any luck on this one yet? I’m having the same issue

Your logout and app.use code with status(404) should go at the bottom after app.listen with PORT || 3000, but this is not relevant.
Open “index.pug” file and check if your h1 tag contains a placeholder text: Home Page. Instead of writing this text you can set a variable, such as home: h1.border.center= home and give its value “Home Page” inside of the object in render function of app.route(/): {…, home: “Home Page”}

Inside of profile.pug body h1 should be set as Profile Page with variable #{profile}, that can be initialized inside of profile route in server or without variable simply with placeholder text Profile Page direct in h1. Also h2 and anchor tags should be written inside of profile.pug as required in “How to Put a Profile Together” challenge.
Some possible issues suggested from other campers:
If you are using mongodb (^3.x.x):
mongo.connect(process.env.DATABASE, (err, client) { let db =client.db(‘yourdb’); }
instead of mongo.connect(process.env.DATABASE, (err, db) {}.
If you are using glitch: cors sould be used before fccTesting(app).
Try to complete all previous “Advanced Node and Express” challenges correct.

With your code is really something not good. You should put all your serialization and other functions and routes, including app.listen, inside of mongo.connect callback function. Look at the line with comment about serialization and app.listen.
Also try to change this:

passport.deserializeUser((id, done) => {
        db.collection('users').findOne(

instead of:

passport.deserializeUser((id, done) => {
        mongo.collection('users').findOne(

and here db.collection, not mongo.collection:

passport.use(new LocalStrategy(
  function(username, password, done) {
    db.collection('users').findOne({ username: username }, function (err, user) {

Don’t forget to introduce variables SESSION_SECRET and DATABASE inside of .env file.