Playing Next Lesson In
seconds

Transcript

  1. So although our cache service works as intended, it's storing information in memory for at least the duration of our server's lifetime, there is a better, more scalable option that we can utilize instead called Redis.

  2. And AdonisJS has first-party support for Redis. Now, Redis has a server of its own, so we're going to need to connect to it through our AdonisJS application.

  3. And in order to do that, we're going to need a way to set connection details based on the environment with which we're in. For that purpose, environment variables exist,

  4. and you can find them within your .env file within your application. If we click into this right now, it's going to be a very bare-bones list

  5. of key-value pairs that we have in this .env style syntax. You have the key on the left-hand side of the equals, and then you have the value on the right-hand side of the equals.

  6. Now, as you can see, so far within this file, we have basic information about the specific environment that we're running within. We have the host that our application is running on,

  7. the desired port that our application should run on, the time zone, log information, the environment itself. So these are all variables that could be different

  8. depending on the specific environment that our application is running on. It could be something on my computer and something entirely different on yours, for example.

  9. And for a fact, the node.env will definitely be switched to production whenever our application is out running on our production server.

  10. In addition to just being environment-specific, they also hold secrets. For example, our app key is a secret that we don't want people to know

  11. because it's used by the encryption module to sign our encrypted values. So if you ever have anything that is environment-specific or a secret that you don't want to be shared with anybody else,

  12. the environment variables is the place that you want to put it. Now, in addition to this key-value pair here, AdonisJS also provides type safety for this via the env.ts

  13. that we saw earlier within our start directory. This is going to define what we expect to be within the .env. And if AdonisJS finds anything within the .env file declaration

  14. that doesn't match our literal .env file, it will fail to boot our application. For example, let's go ahead and dive into our terminal, stop our server.

  15. Let's dive back into our code base, jump into our .env, and let's remove the app key. I'm just going to cut it out for right now. We'll paste it back in whenever we're done.

  16. If we dive into our env.ts now, it has a declaration for the app key that states that it should be a type of string. And our app key also doesn't have optional specified on it, so it is required.

  17. If it were optional, it would look something like this. So let's go ahead and remove that, and let's try to boot up our server. So I'm going to hide our text editor back away to get down to our terminal.

  18. And now that we have removed the app key from our .env file, let's go ahead and try to boot our server up with npm run dev. I'm going to hit Enter. We'll see it run, and look at that.

  19. Our HTTP server has died, though it's still watching for changes. So if we were to fix this error, it would boot right back up. The only reason that it died is because it's missing the environment variable

  20. app key, which is expected because we removed it from our .env file. So the validation failed for one or more environment variables, and it's using that env.ts file within our start directory to check that.

  21. So we have two options here to remedy the issue that we've created. We can either make that environment variable optional, or we can add it back into our .env file.

  22. So let's open our text editor back up. Now the app key is actually something that is required by AdonisJS itself. If we make this optional, it will fix the issue that we're specifically seeing,

  23. but it will create others. So let's go ahead and make this optional so that we can see that this is a valid option to fix this particular issue. So let's give this a save, hide our text editor back away, and there we go.

  24. We see a completely different error, though our server still died. The error is now missing app.app_key. The key is required to encrypt values. Let's dive back into our code base,

  25. remove the optional that we added in because this is something that is required for our application to run successfully. Hence, why we'll want to specify it as required within our env.ts.

  26. We can give our env.ts a save and add our app key back in. Give that a save, hide our text editor back away, and voila, our server is back up and running.

  27. We can verify that within our browser. We saw it do a refresh. We'll go ahead and do a sanity check there, and everything's still A-OK. We can hide that back away, and we still see our cache is getting hit within our terminal.

  28. So in the next lesson, we'll install Redis, configure it within our application, and swap our cache system out so that we are using something a lot more scalable as opposed to

  29. our singleton service within our application.

Environment Variables and their Validation

@tomgobich
Published by
@tomgobich
In This Lesson

We'll learn how we can securely store sensitive and environment-based variables using our .env file. We'll also learn how AdonisJS adds type-safety to these variables using our env.ts file as validation.

Join the Discussion 0 comments

Create a free account to join in on the discussion
robot comment bubble

Be the first to comment!