Build a blog application on Google App Engine: Setup (part 1)

Last June, Google announced the support for Node.js 8 on Google App Engine Standard environment. And with it, the possibility to build highly available Node.js applications and not have to worry about the underlying infrastructure. With this great news, I now had to write this tutorial that had been sitting on my to-do list for months! :)

In this multipart tutorial, we will build a blog application in Typescript, using the Google Datastore to store our content. The Datastore is Google fully managed Document Database with amazing performance, massive scalability, and atomic transactions. I’ve been working with it on multiple projects and I must say that I love it!

The tutorial has been divided into the following 8 sections:

What we’ll be building

Before entering this journey with me you will want to see first what we are about to build… I wouldn’t expect less! :) You can find a live demo of the application running on Google App Engine right here:
https://blog-nodejs.appspot.com

Our goal

By the end of this tutorial you will be able to:

  • Read/Create/update/delete blog posts.
  • Attach a featured image to a post and upload it to Google Storage.
  • Let users comment on a post.
  • Automatically delete the comments and the featured image associated with a post when we delete it from Google Datastore.
  • Deploy the application to Google App Engine.

What we won’t be seeing

To keep the focus of the tutorial on Node.js and Typescript, I won’t be explaining the following:

  • The client javascript code (kept to a minimum to “get the job done”).
  • The SASS styling (most of the styling comes from the great Bulma framework).
  • Express view templates with Pug (not much complexity there but feel free to ask in the comments if something is not clear).

Let’s get started!

Prerequisite:

Node version 8: This project requires Node version 8 or superior. Make sure you have it installed (you can check the node version installed by running node -v in a terminal window).
Google Cloud SDK (gcloud): In case you haven’t done it yet, install the SDK and make sure you are authenticated. To authenticate gcloud run the following command in a terminal:

gcloud auth application-default login

You can then check the account you are authenticated to with:

gcloud config list

Get the source code

Let’s now clone the repository, check-out the starting branch and install the project dependencies. Run the following 4 commands in a teminal:

git clone https://github.com/sebelga/google-datastore-blog-app
cd google-datastore-blog-app
git checkout --track origin/tutorial-start
npm install (or yarn install)

Great, you now have the project structure and the dependencies installed. We will spend most of our time in the “src/server” folder where we will put our Typescript code. 
Let’s launch the application and make sure everything is running fine:

npm run start-local
# or
yarn start-local

This command will compile the Typescript code and start watching both the client and the server code, restarting the Node server if needed.
Open a browser window and navigate to http://localhost:3000. You should see Hello! printed on the screen.

gstore-node

As you probably know the Google Datastore is a NoSQL database. It is Schemaless, meaning that it lets you save any shape of document (called entities) and groups them by Entity Kind. This is great as it gives us a lot of flexibility to save unstructured data. But it also means that you can have two entities of the same kind with completely different properties on each of them.

This is not so great to build complex applications, as those always have an implicit schema on the entities they manage. Meaning, there is no such thing as Schemaless when building an application against a NoSQL database; when you retrieve a “BlogPost” entity from the database, the application expects it to always have a “title” property casted to a string. And this is what Schemas gives us, the guarantee that our entities shape will always be consistent. This is the reason why they are such great help when it comes to building applications against a NoSQL database like the Google Datastore.

The official google-cloud/datastore package to manage your Datastore entities does not provide a way to model the entities through Schemas. This is the reason why I created gstore-node. It does not replace the Google Datastore library but it is a layer on top of it. This means that the Google datastore API is still available in case you might need it.

You will find here the complete documentation of gstore-node.

As you have imagined, we will be using gstore-node to create our Blog Application and hopefully, you will see its benefits when it comes to building Node.js applications using the Datastore.

With that said, let’s jump to the next section where we’ll talk about the application architecture, the different modules, and the application configuration.