jharrilim / app-engine-typescript

Google Cloud App Engine with Typescript/Node

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Deploying to Google App Engine with Typescript/Node

A guide to get you up and running on the Google App Engine using Typescript targetting Node.

Table of Contents

Lexicon

  • .gcloudignore: files to ignore when uploading to Google Cloud. Uses the same syntax as .gitignore.
  • gcloud: CLI interface for Google Cloud, can be obtained here.
  • app.yaml: Configuration for your Google Cloud App Engine app. Reference here.
  • gcp-build: Special node script that Google Cloud runs before it calls npm start.

Prerequisites

  • Cloud SDK CLI tool installed (gcloud)
  • Node installed (10.x+ for this repo)

Steps to MVP

To deploy to Google App Engine with your Typescript project, use the following steps:

Create an app.yaml

The app.yaml contains some infrastructure information for your deployment. You may use something like this:

runtime: nodejs10
instance_class: F1
automatic_scaling:
  max_instances: 1

Note: as of writing, the only node runtimes supported are nodejs10 and nodejs8.

The settings I have chosen keep me within free tier. You may customize it to your liking.

Create a Typescript Project

Create the inital tsconfig.json to get started with Typescript:

tsc --init

Customize the tsconfig.json

We should make a couple modifications to the tsconfig.json. We will want to specify the directory where our code will lie using the rootDir field, and we need to specify a directory to output js files to using outDir. You may output js beside the ts files by not specifying anything, but that gets a little too messy.

Install Some Project Dependencies

Dependencies

npm i express

Dev Dependencies

npm i -D typescript @types/express

Add NPM Scripts

Your package.json should at minimum have these scripts:

"scripts": {
    "start": "node ./dist/index.js",
    "gcp-build": "tsc -p ."
  }

Google Cloud will run your gcp-build script if specified, and use npm start to run your application. Use gcp-build to run your Typescript compiler, or use your own build script. npm start must be able to get into the index.js created by tsc.

Add Some Code

Make a src directory:

mkdir src

Then add some basic server code:

// src/index.ts

import express = require('express');

const port = Number(process.env.PORT) || 8080;

const app = express();
app.enable('trust proxy');


const server = app.use('/', (req, res, next) => {
    res.status(200).send('Hello Google App Engine');
}).listen(port);

Add .gcloudignore

Ignore files you don't want to upload to Google Cloud:

.gcloudignore
.git
.gitignore
node_modules/
scripts
dist

Deploy

You may deploy your app to the Google App Engine using the following:

gcloud app deploy

Note: For this to work, you had to have gone through the process of installing the Cloud SDK CLI. Instructions for setting up the Cloud SDK can be found here.

About

Google Cloud App Engine with Typescript/Node

License:The Unlicense


Languages

Language:JavaScript 89.2%Language:TypeScript 10.8%