Advanced Node and Express - Logging a User Out

'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 help me

This is a very vague way to ask, and difficult for people to debug.

No matter how self-explanatory it seems, you should always do a couple things if you want some one to debug your code.

  1. Explain your use case and logic flow
  2. Actually include the error and exception you are receiving

Now I edited my post