Deploying Node.js with Gemini
1. Deploying Node.js with Gemini
As a software engineer, I like solving problems and creating code. However, the need for boilerplate configurations and remembering deployment commands I don't use often enough, slows me down and takes some of the joy out of the work. Today, we're going to build, test, and deploy a simple Node.js app, and we'll use Gemini Code Assist to help us get up and running quickly, get testing quickly, and deploy our app to the Cloud. We'll be doing this inside of our IDE of choice, which will be VS code for this video. Make sure you have the Google Cloud code extension installed and Gemini enabled in your project to follow along. Let's get started. First things first, we know we're going to need an app.js and a test.js file for running and testing our application. We'll use the terminal to create a folder for our application and those two files to show how Gemini can help us create a project from scratch. We'll be making our app using the express framework. We'll need to install the express and EJS packages. Let's ask Gemini to remind us of the command. After a moment, Gemini gives us information on installing both packages, including a way to only install them for a single project. Since we're just using them for this demo, let's use the save argument to install them locally. Now that those packages are installed, we need to add them to our projects app.js file. Let's use Gemini to make it quick and correct. Gemini provides the code to require the packages and make them available in our file. Let's quickly copy this into our app.js file. Gemini isn't just about chatting, it can also recommend code in line. Let's use this feature to get the recommended boilerplate for initializing the EJS templating engine. All we have to do is write our request in a comment. When we hit Control plus Enter, Gemini will show us its recommendation, sometimes even multiple recommendations. All we have to do is click ''Accept'' and the code is immediately inserted. This is kind of fun. Basically, we can program in pseudocode for these configurations. Let's keep adding some more code, starting with middleware to parse form data. And we'll need to set up the home route. We want a post route called greeting with a parameter for the name of the person to greet. We don't want to forget that the server needs to be started. Finally, let's create a route for greeting that accepts a name parameter. Now that we've got app.js configured, we need to create a package.json file based on it. Let's go back to chat and ask Gemini if it can create one for our existing file. A few moments later, we get a response with the contents for our package.json file. We also get a description of the code so we can understand enough to make any changes. Let's just update the file and copy in that code. In order for our app to work well, we'll need to create our views. Let's create our index and greeting templates inside our views directory. Our index page will be a simple form asking for a name. Our greeting page will use the data it gets from the form to say, hello. Now that we have all our files, we can use NPM start to test that it works. Going to localhost port 3,000 in our browser opens the index page. I'll put in my name and, Hello, Daryl. We've tested that it runs. But what about unit testing? The easier it is to write tests, the more likely they are to get written. Let's see how well Gemini can help us. First, let's make sure our tests will be able to see our app by adding the line module.exports equals app to the end of app.js. Now, let's ask Gemini to generate a unit test for our home route and ensure that the response code is 200. We get a response with all the needed requirements in the code and an explanation of what it does. Not surprisingly, it does what we asked it to do. Let's copy the code into our test.js file we created earlier. From the requirements, it uses the Super Test package, and we'll use the MOCA test framework with that. So let's make sure we have those installed. Now, let's also update our package.json file with our test script and packages in mind. We could ask Gemini how to create our file contents again. But thanks to the previous response we read, we understand that we just need to update the scripts and dependencies sections. We can now run our test with the npm-test command. The test is passing, and we could continue to use Gemini to aid us in writing more tests. Once you have your app working and your tests passing, you'd probably like to know how you can deploy your app to Google Cloud's Cloud Run. Once again, we turn to Gemini to let us know if we can deploy directly and how to do it. We'll run the sample command. When asked, we'll use the US-Central one region and allow unauthenticated invocations, since this is only a test application. After a few minutes, we will be shown a link to where our app is publicly available. And here's the link that we could follow. But we already know what that page looks like. Gemini helps with many different aspects of development so that you can focus on solving more interesting and more difficult problems.2. Let's practice!
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.