How to create, deploy and update NodeJs project on Google App Engine.
From this article, I am going to discuss how to create, deploy and update a NodeJs project on Google App Engine. There are five sections that describe the process.
- Install Google Cloud SDK
- Create Google Cloud Project and Enable App Engine
- Write Simple NodeJs application
- Deploy NodeJs project to App Engine
- Update and re-deploy project
If you already installed Google Cloud SDK and have created the Google Cloud project skip the first two sections.
1. Install Google Cloud SDK
Follow the link below to install the Google Cloud SDK on your computer.
After successful installation;
- Open the terminal
- Run
gcloud auth login
command - This command will open your default browser and log in to your GCP account and select the corresponding gmail account.
I will not go into more detail on this as it is an easy process.
2. Create Google Cloud Project and Enable App Engine
First, you need to create a Google Cloud Project and activate the App Engine. Follow the steps below to create a Google Cloud Project.
- Open this link using a browser.
2. Enter a project name and create a project by clicking the Create button.
3. After creating a Google Cloud Project we need to enable the App Engine. Follow the below link to enable the App Engine.
4. Select a region to serve the app and click next.
4. After enabling App Engine you can see the below screen.
You have now successfully created a Google Cloud Project and enabled the App Engine.
You can find the project ID by clicking on the project name and it is important to remember the project ID for the deployment process.
3. Write Simple NodeJs application
You need to have NodeJs 10 or the latest version installed on your computer to create a web service that can run on the App Engine. It is better to have the NodeJs LTS version on your computer. You can find it here
Download | Node.js
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
nodejs.org
This is the minimum file structure of a simple NodeJs application that can be deployed on the App Engine.
my-node-project/
package.json
app.js
app.yaml
Follow the steps below to create a sample NodeJs application.
- Create a folder named my-node-project
- Navigate to the created folder and open the terminal and run
npm init
command to initialize a Node project
* Keep the default package name and version by pressing enter.
* Add some small description, here I gave it as simple node project
* Change entry point to app.js
* Test command, git repository, key words, author and license keep as default by pressing enter.
3. Then run npm install express
command to add the express dependencies.
4. Create the app.js file and add the following code
5. Add start script to the package.json
"scripts": {
"start": "node app.js"
}
Final package.json
file.
6. Run npm start
command to run the project locally.
7. Go to http://localhost:8080
URL to check if the code works correctly.
8. The last step is to add app.yaml
configuration file to configure your project runtime environment on the App Engine.
If you are using the Node.js LTS version your app.yaml
file needs to be like the following
Or follow the link below to find the NodeJs standard configurations
4. Deploy NodeJs project to App Engine
You must have the Google Cloud SDK installed on your computer to deploy the App Engine project.
Open the terminal in your project folder and type the following command to deploy the project.
gcloud app deploy --project [project_id]
ex-: gcloud app deploy --project my-node-project123
It will take few minutes to deploy the project on the server. After successful deployment, you can find the URL for the project that appears on the terminal. Run the following command to open the deployed project using a browser after the successful deployment.
gcloud app browse --project=[project_id]
ex-: gcloud app browse --project=my-node-project
5 . Update and re-deploy project
In fact, it is similar to deployment. Once you have modified the code, all you need to do is run the gcloud deploy --project [project_id]
command.
Here is the full code of the project
Thanks to Thushara Sampath for helping me write a better article by proofreading.
Thanks for reading.