run-meteor-on-google-app-engine
Learn how to deploy a Meteor app to App Engine flexible environment.

Meteor

​Meteor is an open source platform for web, mobile, and desktop.
– meteor.com
You can check out Node.js and Google Cloud Platform to get an overview of Node.js itself and learn ways to run Node.js apps on Google Cloud Platform.

Prerequisites

    1.
    Create a project in the Google Cloud Platform Console.
    2.
    Enable billing for your project.
    3.
    Install the Cloud SDK.

Prepare

    1.
    ​Install Meteor on your local machine.
    2.
    Create a MongoDB instance as described here. Remember your MONGO_URL, you will need that later. An example MongoDB URI would be mongodb://username:[email protected]:port.

Create

    1.
    Initialize a Meteor project by running the following commands:
    1
    meteor create [YOUR_APP_NAME]
    2
    cd [YOUR_APP_NAME]
    3
    meteor add reactive-dict
    4
    meteor remove autopublish
    Copied!
    replacing [YOUR_APP_NAME] with your app name.
    2.
    To add database functionality, edit [YOUR_APP_NAME]/client/main.js to look like:
    1
    import { Template } from 'meteor/templating';
    2
    import { ReactiveVar } from 'meteor/reactive-var';
    3
    import { Meteor } from 'meteor/meteor'
    4
    ​
    5
    import './main.html';
    6
    ​
    7
    Template.hello.onCreated(function helloOnCreated() {
    8
    // counter starts at 0
    9
    this.counter = new ReactiveDict({value: '0' });
    10
    var instance = this;
    11
    ​
    12
    Meteor.subscribe('counters', function () {
    13
    var counterConn = new Mongo.Collection('counters');
    14
    instance.counterConn = counterConn;
    15
    ​
    16
    var counterList = counterConn.find({}).fetch();
    17
    var dbCounter = counterList[0];
    18
    instance.dbCounter = dbCounter;
    19
    ​
    20
    instance.counter.set('_id', dbCounter._id);
    21
    instance.counter.set('value', dbCounter.value);
    22
    });
    23
    });
    24
    ​
    25
    Template.hello.helpers({
    26
    counter() {
    27
    return Template.instance().counter.get('value');
    28
    },
    29
    });
    30
    ​
    31
    Template.hello.events({
    32
    'click button'(event, instance) {
    33
    ​
    34
    // Increment counter
    35
    instance.counter.set('value', instance.counter.get('value') + 1);
    36
    ​
    37
    // Update counter on DB
    38
    instance.counterConn.update(instance.dbCounter._id, {
    39
    '$set': {'value': instance.counter.get('value') }
    40
    });
    41
    },
    42
    });
    Copied!
    3.
    Edit [APP_NAME]/server/main.js so that it looks like:
    1
    import { Meteor } from 'meteor/meteor';
    2
    import { Mongo } from 'meteor/mongo';
    3
    ​
    4
    Meteor.startup(() => {
    5
    // code to run on server at startup
    6
    const Counters = new Mongo.Collection('counters');
    7
    ​
    8
    // Make sure a Counter entry exists
    9
    if (Counters.find({}).fetch().length == 0)
    10
    Counters.insert({value: 0})
    11
    ​
    12
    // Publish counters
    13
    Meteor.publish('counters', function () {
    14
    return Counters.find({});
    15
    })
    16
    });
    Copied!

Run

    1.
    Run the app with the following command:
    1
    MONGO_URL=[MONGO_URL] meteor run
    Copied!
    replacing [MONGO_URL] with your MongoDB URI.
    2.
    Visit http://localhost:3000 to see the Welcome to Meteor! message.
    When you're done, use CTRL+C to exit Meteor.

Deploy

    1.
    Add the following to the package.json file:
    1
    "scripts": {
    2
    "cleanup": "rm -rf ../bundle/",
    3
    "dist": "npm run cleanup && meteor build ../ --directory --architecture os.linux.x86_64 --server-only",
    4
    "predeploy": "npm run dist && cp app.yaml ../bundle/ && cp Dockerfile ../bundle/",
    5
    "deploy": "npm run predeploy && (cd ../bundle && gcloud app deploy -q)"
    6
    },
    Copied!
1
These scripts provide you with some tasks that prepare the app for
2
deployment to the App Engine flexible environment. See
3
[Custom deployment][custom] for more information about custom Meteor
4
deployments.
Copied!
    1.
    Configure a custom runtime by running the following command:
    1
    gcloud beta app gen-config --custom
    Copied!
    2.
    Replace the contents of the Dockerfile file with the following:
    1
    FROM gcr.io/google_appengine/nodejs
    2
    COPY . /app/
    3
    RUN (cd programs/server && npm install --unsafe-perm)
    4
    CMD node main.js
    Copied!
    The custom Dockerfile is required in order to properly build the Meteor app in production.
    3.
    Add the following to the generated app.yaml file:
    1
    env_variables:
    2
    ROOT_URL: https://[YOUR_PROJECT_ID].appspot-preview.com
    3
    MONGO_URL: [MONGO_URL]
    4
    DISABLE_WEBSOCKETS: "1"
    Copied!
    replacing [YOUR_PROJECT_ID] with your Google Cloud Platform project ID and [MONGO_URL] with your MongoDB URI.
    4.
    Run the following command to deploy your app:
    1
    npm run deploy
    Copied!
    5.
    Visit https://[YOUR_PROJECT_ID].appspot.com to see the Welcome to Meteor! message, replacing [YOUR_PROJECT_ID] with your Google Cloud Platform project ID. To test database functionality, click on the button a few times and refresh the page. You should see your previous button-click count appear after a few seconds.
Last modified 7mo ago