Quality Assurance: Advanced Node and Express

I’m on the section that requires a database to be set up with Mongo. I did that, and pasted my key on the MONGO_URI variable inside the sample.env file, but I’m still getting an error when trying to pass the tests.

Can someone help me find out what I’m doing wrong?

By the way, my password for the database contains a hashtag, and I’ve read that needs to be written as %23 instead.

PORT=8080
NODE_ENV=development
MONGO_URI='mongodb+srv://joshhayles:<password_with_hash%23>@express-test.dt5th.mongodb.net/?retryWrites=true&w=majority'

Hello there,

I have edited your post to remove your password. With it, anyone can read/write to your database.

I suggest you change your password, as this forum is publicly available.


Two suggestions:

  1. Add your database name in the URI: ...mongodb.net/<dbname>?retryWrites=... (replace <dbname> with something like test, if you want)
  2. Remove the ' (single-quotes) wrapped around the URI

If that does not work, would you share your code (GitHub repo, or Replit link, or similar)?

Thank you for the reply and help. I’m still not able to get it going. Now, when I run npm start I’m getting these errors:

I also updated the code, but I don’t know if it’s correct:

PORT=8080
NODE_ENV=development
MONGO_URI=mongodb+srv://joshhayles:<password>@express-test.dt5th.mongodb.net/express-test?retryWrites=true&w=majority

You should not add it to the sample.env file. You should create a copy of that file, name the copy .env, and put it in that new file.

It still isn’t working. And I’m not sure what creating a copy of the same file type will do. There’s nothing in these instructions that say to do that.

I’m honestly disappointed in the lack of instructions for these exercises.

I’m still getting the same errors as above …

It looks like you’re working on the challenges locally, which I’ll admit isn’t something they’re specifically designed for. That is, there are some extra steps that need to be taken.

Copying the sample.env file to the .env file is pretty common with projects - The sample.env file is designed to give a boilerplate for the secrets you’ll need for the project, and the .env file is typically ignored by version control so those secrets aren’t committed publicly. This isn’t something that I believe is mentioned in our curriculum, and it probably could be.

One thing that is covered, however, is the use of the dotenv package when working locally - it’s mentioned in this lesson. Have you installed and configured that package?

Okay. If .env files are typically ignored, how will the one I copied be found?

They are ignored by version control (typically), so they wouldn’t be committed to a GitHub repository for example.

The dotenv package, however, looks for a .env file by default.

Okay. The instructions say to import the variables on app.js, but I don’t have that file in this lesson …

If you are working locally, you will need the dotenv package. It loads environment variables from your .env file into process.env . Install it with npm install dotenv . Then, at the top of your myApp.js file, import and load the variables with require('dotenv').config() .

That’s an instruction specific to that challenge.

You’d run require('dotenv').config() in the entry file for your current project.

What’s the entry file?

I actually have that already at the top on my server.js

Okay, that’s correct.

And your .env file is in the same root directory as your sample.env file?

You want the .env file in the same folder as the sample.env

The sample.env file is not inside a folder. It was just created from the root folder freeCodeCamp …

So you’re not making a copy of the sample.env file with the same name… you need a file just called “.env”… nothing in front. That is a special file for storing “environmental variables”, which are secret things like passwords and database access stuff… that way someone can’t just view the source code of your program, and get access to all your secret stuff.

So, in the same place that your “sample.env” file is, create a new file, and just call it “.env”… it should then have a settings gear as an Icon… then copy the contents of sample.env into the .env file. Then that file is where you would also put your MONGO_URI.

In your programming code, anytime you type process.env you are accessing that .env file.

Also, as a note, once you have a “.env” file, and have put the necessary stuff in it, I’d suggest deleting your “sample.env” file, as it currently has some secret stuff in it.

Also, in this thread, might want to delete the picture you sent as it also has your database login info in it… that’s how you end up with a database full of Russian links and characters… like happened to one of my Joomla SQL databases, lol.

Thank you for the help. I just renamed (took away ‘sample’) from that .env file to change the name. It’s still not working, is that not okay to do?

Are you able to share all of your code? A link to a GitHub repo would be ideal. Otherwise, if you import your code into Replit and share that link, that would be easiest.

The main points in this thread:

  1. The sample.env file should not be used (read) by any application - it serves as a boilerplate for developers to know what environment variables (secrets) are needed for an application.
  2. The .env file should not be committed in Git, and often contains secrets that should not be shared with anyone. In order to make use of the .env file, you usually have to use a library like dotenv to parse (read) the file, and inject the secrets into your Nodejs process (process.env). I say ‘usually’, because environments like Replit do this automatically
  3. As you can see in your code, your app refers to a SESSION_SECRET. This should be added to your .env file.

Hope this helps

Here’s the SSH key. Will this allow you to see all the code?

git@github.com:joshhayles/advanced-node-qa.git