Quality Assurance: Advanced Node and Express

Thank you. This is what I was looking for: GitHub - joshhayles/advanced-node-qa: FreeCodeCamp QA Certification

Have you ensured to follow this lesson correctly?

I’ve been trying my best to follow along, but there are obviously some holes that I’m not seeing. I’m really stuck. I’ve tried everything I can. I’m almost done with this course, and I would really like to see it through for the certificate

As mentioned previously, did you add a variable “SECRET_SESSION” to your .env file as mentioned in the instructions?

Be sure to add ‘SESSION_SECRET’ to your .env file and give it a random value.

This variable is used kinda like a hash for the login session. You just need it to be assigned a random value… I used ‘helloworld’, but you could just assign it anything. Your .env should have the stuff that sample.env had, plus the MONGO_URI and the SESSION_SECRET… if that all looks right, if there is still problems, please share what errors you are getting. You could also share the contents of your .env file, just make sure to delete or scratch out any password information from the MONGO_URI before posting.

Still not working … here’s what’s happening after I run ‘npm start’

I see “authentication failed”… so the first question… does your MONGO_URI say <password> in it when you run your code, or did you just change that so your picture wouldn’t include your password… its good not to show your password when sharing a picture, but when you run your code you need to make sure you replace <password> with your database password.

That’s a good catch. I changed the password, ran npm start again and it’s still not working. The error message went away, but still not working.

Also, here’s my server.js file:

'use strict';
require('dotenv').config();
const express = require('express');
const myDB = require('./connection');
const fccTesting = require('./freeCodeCamp/fcctesting.js');
const session = require('express-session');
const passport = require('passport');
const ObjectID = require('mongodb').ObjectID;

const app = express();
app.set('view engine', 'pug');

fccTesting(app); // For fCC testing purposes
app.use('/public', express.static(process.cwd() + '/public'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
  cookie: { secure: false }
}));

app.use(passport.initialize());
app.use(passport.session());

myDB(async (client) => {
  const myDataBase = await client.db('database').collection('users');

  // Be sure to change the title
  app.route('/').get((req, res) => {
    // Change the response to render the Pug template
    res.render('pug', {
      title: 'Connected to Database',
      message: 'Please login'
    });
  });

  // Serialization and deserialization here...
  passport.serializeUser((user, done) => {
    done(null, user._id);
  });
  passport.deserializeUser((id, done) => {
    myDataBase.findOne({ _id: new ObjectID(id) }, (err, doc) => {
      done(null, doc);
    });
  });
  // Be sure to add this...
}).catch((e) => {
  app.route('/').get((req, res) => {
    res.render('pug', { title: e, message: 'Unable to login' });
  });
});
// app.listen out here...

app.listen(process.env.PORT || 3000, () => {
  console.log('Listening on port ' + process.env.PORT);
});

Are you sure it’s not working… what challenge are you on and have you tried submitting the code?

If you’re on the Set up Passport challenge, I don’t think its supposed to do anything spectacular… it says its currently listening on port 8080, so if in a browser you type https://localhost:8080 you can see if it loads the pug page… and you can try submitting the code to the challenge website to see if you pass.

Even at 8080 it’s not working. And yes, I’m on the passport challenge. When I submit my URL it’s failing both tests.

So, here is where deciding to do the project on your local machine instead of using a Replit can cause issues, because we can’t troubleshoot your system or your internet connection, or see for ourself what exactly is going on.

I created a new Replit with the boilerplate, added your package.json, and the code you provided above for the server.js, added my MONGO_URI and a SESSION_SECRET of helloworld to Replits secrets list, and the page ran, and I passed the “Set Up Passport” Challenge.

Some troubleshooting steps you could try… I’m not sure if you should use http:// or https:// in your browser, you should try both… maybe try changing port=8080 to port=3000 in the .env file… maybe your computer is blocking 8080 for this. If you changed any files aside from what I mensioned above, verify the code there… aside from that hard to tell if its something on your system, browser, some conflicting application, but your code seems to run fine on replit now that we fixed the ENV stuff.

Maybe someone else has an idea.

Also, just noticed you were actually farther than Set Up Passport, but my new replit with your code was also able to pass the challenge I think you’re on:

Okay! I got everything set up using a replit and I was able to get past the passport lesson. Thanks to you, and everyone else for working through this with me. If I may give some feedback (if anyone working for FCC is listening) I think there should be more thorough instructions/troubleshooting for these lessons.

Thanks again!

I spoke too soon … I’m still not able to pass the database connection test …

Not sure if you know how .env works on Replit. Did you setup the secrets?

On a server or working on your PC, to hide secret values you use your .env file… well, most online services, like Replit, doesn’t use .env because you’d be uploading all your secrets in a file meaning people can read it. Instead, Replit uses what it calls Secrets for its system environmental variables. It’s the lock icon on the left:

image

So when using Replit… anything you would normally put in your .env file, instead you add to the list of variables on that page. Does it still not work if you do that?

I was able to figure out how to add the Secrets on replit, but it’s still not working. Here’s a screenshot of mine when I click on raw editor.

I also pasted my URL to the replit. Isn’t that public so you can see those files?

  1. You need to either use
app.route('/').get((req, res) => {

Or

app.get('/', (req, res) => {
  1. The title has to be title: 'Connected to Database',

Just to be super clear in case it isn’t. I will blur it to not spoil it.

app.get('/', (req, res) => {
  // Change the response to render the Pug template
  res.render('pug', {
    title: 'Connected to Database',
    message: 'Please login'
  });
});

I changed those items and it’s still not working …

The error MongoServerSelectionError seems related to the actual connection to the DB. It mainly seems to happen when your IP changes so you have to update the IP Access List.

If you only added your current IP you might try adding 0.0.0.0/0 (all) instead.


Other than that, I’m not sure what the problem is.

THAT WORKED!!! Thank you so much!

Great, happy we got it fixed. :rocket: