A guide to get you up and running on the Google App Engine using Typescript targetting Node.
- .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
.
- Cloud SDK CLI tool installed (gcloud)
- Node installed (10.x for this repo)
To deploy to Google App Engine with your Typescript project, use the following steps:
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
andnodejs8
.
The settings I have chosen keep me within free tier. You may customize it to your liking.
Create the inital tsconfig.json to get started with Typescript:
tsc --init
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.
npm i express
npm i -D typescript @types/express
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 usenpm start
to run your application. Usegcp-build
to run your Typescript compiler, or use your own build script.npm start
must be able to get into theindex.js
created bytsc
.
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);
Ignore files you don't want to upload to Google Cloud:
.gcloudignore
.git
.gitignore
node_modules/
scripts
dist
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.