Now offers a friction-free way to deploy node applications right from the terminal. In this lesson, we'll learn how to use the now CLI to deploy a node application, including the deployment of environment variables. We'll also look at how now retains each version of your application, how to see the running code right in the browser and how to remove a deployment when you no longer want that version.
I'm going to start by installing the Now CLI globally using npmi -g Now. With Now installed, let's take a look at our simple application.
In add-on, you'll see that I installed Express and created a basic Hello World Express app. In order to deploy this Express application to Now, I need to add a start script to my package.json. So I'll jump down to the script section here. Start script, and I just want to run node with index.js.
With the application ready to go, I'm going to jump back into the terminal, and I want to issue the Now command. The first time you run the Now command, you're going to be promoted for an email address. You'll get an email, and it'll have a link in it. You can click the link to get your authentication token.
I'm already logged in, so when I run this, it's just going to start my deployment. I'll press Enter, and Now is going to create a URL for me and put it on my clipboard right away.
It's still deploying, but I can go to the browser right now, and I can paste that URL into the address bar. I'll actually see Now going through the deployment steps.
When it finishes, Now is going to run my start script that I added to package.json. We'll see our application get updated as soon as it's deployed, right in the browser.
One of the cool features of Now is because this is a public project, I can actually go into this URL and add /_source, and I can see the source code for the application I just deployed. I can look at my package.json. I can come over here and click index.js and actually see the application code.
All right, let's go make a change to our application.
I'm going to open index.js. In my greeting, I'm going to replace the word "now" with an environment variable. I'm going to switch out these single quotes for backticks, and replace "now" with some string interpolation. I'm just going to reference process.env.greeting.
Where does this environment variable come from? When we deploy to Now, we have the option to send an environment variable with a value. I want to issue the Now command again. This time I'm going to pass -e, which is going to allow me to set an environment variable with it's value.
I'll press Enter, and I'll get a new URL. I'm just going to jump into the browser, and I'm going to paste the updated URL. Just like before, I can watch Now do it's thing. When it's finished, we'll see that our page is now displaying the greeting that uses that environment variable.
If I click the back button on my browser, you'll see that the old version of the application is still available. Every time I make a change and deploy to Now, it's going to give me an updated URL that represents a snapshot of the version I'm deploying at this point in time. So, I can actually cycle through different versions just by using an updated URL.
If I take a look in the terminal and run the Now ls command, I see that I can get a list of my deployments. I have the same project, which is in a directory called Now Demo. It's been deployed two separate times, one a while back at the beginning of the video. That was the initial version of this application. Then, the one that we just deployed with the environment variable.
I can keep both version around if I want. But let's say I'm done with the original version, and I only want to use the code that now uses that environment variable. Well, I can use Now rm, and I can pass it this ID, and I can remove a deployment.
It'll ask me if I'm sure. If I want to keep this, obviously I can respond with no. But I'm going to go ahead and delete it. So, I'll hit yes. If I run ls one more time, we'll see that I only have a single deploy of this single project.
Now has quite a few options. If you want to see everything that's available, you can just run Now help, and you'll get a list of all the options for Now.