Please help pass ||Create New Middleware

Tell us what’s happening:
My code looks right, but still cannot pass it:

Your code so far

Link to the challenge:

Shouldn’t it just be this without the extra parameter you are using:

 .get(ensureAuthenticated, (req,res) => {
    res.render(process.cwd() + '/views/pug/profile');

Can’t really test it because of the private .env variables. Also, when posting the link it is better if you post the edit link and not the full page.!/daily-waiting-tartan

Thank you for your feedback. I have removed the extra parameter and the issue persists. The reason I am not passing is because:

A Get request to /profile should correctly redirect to / since we are not authenticated.

How to redirect to “/” in the Get request to /profile? Can you help?

Would changing the parameter with the following one help?

    .get(ensureAuthenticated, (req, res) => {
  res.render(process.cwd() + '/views/pug/profile', {
        failureRedirect: '/'

In the previous one the type of issue to overcome was:

A POST request to /login should correctly redirect to /.

and the code to pass it was:

    .post(passport.authenticate('local', { failureRedirect: '/' }), (req, res, next) => {

now I am thinking that by adding similar piece of code now for Get should work or no?

    .get(passport.authenticate('local', { failureRedirect: '/' }), (req, res, next) => {

Not really sure, in this checkpoint file it shows it with the extra parameter. Maybe try checking your work against it.

As I said, it is hard for me to test and I’m about to head out, sorry.

1 Like

The above mentioning “homepage” is the key passing this one. You just have to add a mentioning to “Home Page” in the index.pug file and this solves the problem. So basically change the following line from

meta(name='description', content='A cool thing made with HyPerDev. ') 

to the following:

meta(name='description', content='A cool thing made with HyPerDev. 
This is also the Home page')



That’s unfortunate and extremely obscure. If the test is looking for the “Home page” string on the index page to test the redirect, then why on earth does the pug file in the starter template not have that?

There is no way reading the challenge requirements will let you know that you have to add that string to the index.pug file. The line “redirect to our homepage” is just a way of saying redirect to the root. It does in no way relate to the text on the page. It also doesn’t seem like a very reliable way of testing the redirect.

May I know how you overcome the "A POST request to /login should correctly redirect to /." problem? I keep encountering this error with the following code:

"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.urlencoded({ extended: true }));

  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,


mongo.connect(process.env.DATABASE, {useUnifiedTopology: true}, (err, client) => {
  var db = client.db("test");
  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) => {
        {_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) => {
      //Change the response to render the Pug template
      //res.send(`Pug template is not defined.`);
      res.render(process.cwd() + '/views/pug/index', {title: 'Hello', message: 'Please login',showLogin: true});

    //app.route("/login").psot( passport.authenticate('local', { failureRedirect: '/login' }),
        .post(passport.authenticate('local', { failureRedirect: '/' }), (req, res, next) => {

          .get((req,res) => {
               res.render(process.cwd() + '/views/pug/profile');
    app.listen(process.env.PORT || 3000, () => {
      console.log("Listening on port " + process.env.PORT);

and it always returned “A POST request to /login should correctly redirect to /. (Test timed out)”. My code is host in