Web development & API design

L013: Ready for takeoff! Deployment & publishing

Deployment

  • Make your app available to the world!
     
  • Pretty easy these days
     
  • … and also free (Heroku, Modulus, now.sh, …)

Alternatives

  • Self-hosted
     
  • "Cloud"
    • IaaS
      • Google Cloud Engine
      • Amazon Web Services (AWS)
      • Microsoft Azure
    • PaaS
      • Heroku
      • AWS
         
  • Whatever "serverless" or "function as a service" means

Deploy to Heroku!

Heroku config

  • Process types
    • What type of application
    • Which command to run to start
       
  • Heroku detects Node.js
  • Dependencies are auto-installed
web: node server.js

./Procfile

How to deploy?

  • Heroku git server
  • Your machine
  • Heroku cluster
  • Heroku config parser

How to deploy?

  1. (Install heroku toolbelt)
     
  2. git init
  3. git add --all
  4. git commit -m "Initial commit"
  5. heroku create
  6. git push heroku master

Connect to a DB

  • Can't use local DB for hosted app (duh)
     
  • mLab is cool (and free)

Some config vars

  • PORT
     
  • DB_URL
  • DB_PASSWORD
  • Abstracts your executable
     
  • Prepackages all dependencies
    • Node, node_modules/, …
       
  • Run anything with docker run <image>
  • Basically the new way to deploy
     
  • Kubernetes/AWS/Azure/Heroku/…
     
  • Easier local runtime

Dockerfile

FROM node:8-alpine

COPY server.js .
COPY package.json .
COPY yarn.lock .

RUN yarn

CMD ["node", "server.js"]
  • How to build your image
    • Steal FROM someone
    • COPY files
    • RUN commands
       
  • How to run it
    • CMD: Execute your app

Deploy with Docker

  1. heroku container:login
     
  2. heroku container:push web
     
  3. ???
     
  4. heroku open

 

(Disables pushing to Heroku remote)

Publish to npm!

or:

 

Become part of the problem! It's easy and free!

Things to think about before publish

  • Has this been solved before?
  • Can I contribute this to an existing project?
  • Will this be useful to anyone else (or me in another project)?
  • Does the code contain
    • tokens/passwords
    • business critical data
  • Will I bother to maintain this?
  • Code quality assurance (QA)?
    • Unit tests (Jest)
    • Coverage (thanks, Jest)
    • Linting (ESLint)

package.json

  • Fields matter!
    • name (unique)
    • version (use semver)
    • licence (don't be React)
       
  • Dependencies
    • It won't work without them!
    • devDependencies aren't installed
       
  • README.md is displayed on npm
{
  "name": "two-string",
  "version": "1.0.2",
  "description": "An incredibly useful twoString utility.",
  "keywords": [
    "two",
    "string",
    "twoString"
  ],
  "homepage": "https://github.com/theneva/two-string",
  "bugs": "https://github.com/theneva/two-string/issues",
  "license": "CC0-1.0",
  "author": "Martin Lehmann <martin@lehmann.tech> (https://github.com/theneva)",
  "main": "src/index.js",
  "repository": {
    "type": "git",
    "url": "https://github.com/theneva/two-string"
  },
  "dependencies": {},
}

./package.json

package.json

  • "main": "path"
    • Exports from <path> are returned from
      require('two-string')
       
    • npm init assumes index.js
{
  "name": "two-string",
  "version": "1.0.2",
  "description": "An incredibly useful twoString utility.",
  "main": "src/index.js",
}

./package.json

Test your package in another project

  • In package dir: yarn link
  • In other project: yarn link <package>

Content? Publish!

  • yarn login
  • yarn publish

Exercise

  1. Deploy the colors app to Heroku
    1. Run locally
    2. Push to Heroku
       
  2. Package the app as a Dockerfile
    1. (Install Docker)
    2. Build docker image (with Dockerfile)
       
  3. Deploy the Dockerfile to Heroku

PG6300-17-013 Ready for takeoff!

By theneva

PG6300-17-013 Ready for takeoff!

  • 776