Giter Club home page Giter Club logo

test-deploy3's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.