This small application shows you how to use
gstore-node to build an application on Google App Engine standard environment.
gstore-node is a modeling library that lets you define Schemas for your entities to easily validate their data before saving them in Google Datastore. It has also other useful features like pre & post middleware, virtual properties or a cache layer to speed up entities fetching.
I wrote a multi-part tutorial on Medium that goes through the code in this repository. You can fint it here
You can see a live version of the application at the following url: https://blog-nodejs.appspot.com/blog.
Play with it as much as you want but don't feel sad if your curated post disappears as a Cron job cleans up the posts every 24h :smile:.
Before starting make sure that you have the necesary dependencies installed on your system.
gcloudcli on your $PATH
Once you have installed the Google SDK, make sure that you are authenticated. In your terminal run
gcloud auth application-default login
A window browser should open, allowing you to authenticate the Google Cloud SDK.
Before starting, create a projet in the Google Cloud Platform to deploy the application.
Go to App Engine and create a new project if you don't have one.
You then have to make sure to:
Enable billing for the project (Enable billing)
Enable the Google Cloud Datastore API (Enable the API)
Set the project as default in gcloud
gcloud config set project <your-project-id>
Once you have your project configured in Google Cloud, install the application dependencies with
npm install # or yarn
The application needs a few environment variables to be defined. For local development those variables are defined in a
.env file. Rename the example.env file to .env. Make sure to define the
GCLOUD_BUCKET variables. This
.env file sould not be commited and pushed to source control.
# ------------------- # Server # ------------------- ## Server port (optional. Default 8080) PORT=3000 # ------------------- # Google Cloud # ------------------- GOOGLE_CLOUD_PROJECT=<your-google-cloud-project> GCLOUD_BUCKET=<your-google-storage-bucket> ## Namespace for the Datastore entities (optional) DATASTORE_NAMESPACE=development ## Local Datastore Emulator Host (optional but recommended for development) # DATASTORE_EMULATOR_HOST=localhost:8081 # ------------------- # Misc # ------------------- ## Enable Logger (optional. Default "true") LOGGER_ENABLED=true ## Logger level (optional. Default "info") ## Allowed values: 'error', 'warn', 'info', 'verbose', 'debug', 'silly' LOGGER_LEVEL=info
To be able to execute the Datastore Queries of this application, you will need first to update the Datastore indexes with the command below. For more information about indexes, read the documentation.
gcloud datastore create-indexes ./index.yaml
You can now navigate to
http://localhost:3000 and start creating posts and comments.
Before deploying the application make sure you have defined the
GCLOUD_BUCKET environment variable in the
app.yaml file. Then, to deploy the application, run the following command:
npm run deploy -v <app-version> # or yarn deploy -v <app-version>
This script will build the client + server code and deploy the application on Google Cloud. But it will not promote the traffic to the specified version. This allows you to first test your application and make sure that everything run correctly.
Once you are ready to send the traffic to the new version, simply run:
npm run promote -v <app-version> # or yarn promote -v <app-version>
Sébastien Loix – @sebloix
Distributed under the MIT license. See
LICENSE for more information.