Before you begin
Before running and deploying this sample, take the following steps:-
Use the GCP Console to create a new GCP project,
create an App Engine application, and enable billing:
Go to App Engine When prompted, select the region where you want your App Engine application located and then enable billing.
- Install the following prerequisites
locally:
- Download and install git.
- Prepare your environment for Node.js development.
-
Download and install the Google Cloud SDK and then initialize the
gcloudtool:
Download the SDK
-
Alternatively: You can use
Google Cloud Shell, which
comes with git and Cloud SDK already installed, and
many other features, such as language
support and code editors.
After Google Cloud Shell is launched, you can proceed with the rest
of this tutorial and execute your commands within Cloud Shell.
Download the Hello World app
We've created a simple Hello World app for Node.js so you can quickly get a feel for deploying an app to the App Engine flexible environment. Follow these steps from a command line to download Hello World to your local machine.Download the sample app and navigate into the app directory:
-
Clone the Hello World sample app repository to your local machine:
Alternatively, you can download the sample as a zip file and extract it.git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
-
Change to the directory that contains the sample code:
cd nodejs-docs-samples/appengine/hello-world/flexible
Run Hello World on your local machine
-
Install dependencies for this project by entering the following command:
or if you are using Yarn:npm install
yarn install -
Run the start script.
or if you are using Yarn:npm start
yarn start -
View your app in your web browser:
-
If you are using Cloud Shell, in the toolbar, click
Web Preview
and
select Preview on port 8080 instead.
-
If you are not using Cloud Shell, enter the following address:
http://localhost:8080
-
If you are using Cloud Shell, in the toolbar, click
Web Preview
In your terminal window, press Ctrl+C to exit the web server.
Deploy and run Hello World on App Engine
To deploy your app to the App Engine flexible environment:- Deploy the Hello World app by running the following
command
from the
hello-world/flexibledirectory:
gcloud app deployLearn about the optional flags. -
Launch your browser and view the app at
http://YOUR_PROJECT_ID.appspot.com, by running the following command:
gcloud app browse
See the following sections for information about cleaning up as well as links to the possible next steps that you can take.
Clean up
To avoid incurring charges, you can delete your GCP project to stop billing for all the resources used within that project.-
In the GCP Console, go to the Projects page.
Go to the Projects page
-
In the project list, select the project you
want to delete and click Delete project.
- In the dialog, type the project ID, and then click Shut down to delete the project.
What's next
Learn the whole platform
Now that you know what it's like to develop and deploy App Engine apps, you can stretch out and see the rest of Google Cloud Platform. You already have the Google Cloud SDK installed and that gives you the tools to interact with products like Google Cloud SQL, Google Cloud Storage, Google Cloud Datastore, and more. For a guided walkthrough that teaches you how to create an application that uses the entire platform, not just App Engine, check out our quickstart on creating the Bookshelf app.Learn about the App Engine flexible environment
Here are some topics to help continue your learning about App Engine.Hello World code review
Hello World is the simplest possible App Engine app, as it contains only one service, has only one version, and all of the code is located within the app's root directory. This section describes each of the app files in detail.
app.js
The app.js file contains the JavaScript code to start a server and respond to
requests.The following code responds to requests from web clients by displaying the 'Hello World' message, via a server that runs on port 8080.
app.yaml
The app.yaml
file describes an application's deployment configuration:Here,
app.yaml specifies the runtime used by
the app, and sets env: flex, specifying that the app
uses the flexible environment.-
For more information on how the Node.js runtime works, see
The Node.js Runtime.
-
For more details about how to design your app to take advantage of versions
and services, see An Overview of App
Engine.
-
For more details about the configuration settings for App Engine, see
Configuring Your App with
app.yaml.
package.json
package.json is used to
specify application dependencies, node version, and application startup.This package.json specifies that the application uses Node.js v4.3.2 or later, depends on express, and that
node app.js is
used to start the application. For more details about how your application is
installed and run, see the Node.js runtime
documentation.
No comments:
Post a Comment