Powered By GitBook
run-hapijs-on-google-app-engine
Learn how to deploy a Hapi.js app to App Engine flexible environment.
Contributed by Google employees.
You can check out Node.js and Google Cloud to get an overview of Node.js itself and learn ways to run Node.js apps on Google Cloud.

Prerequisites

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

Prepare

    1.
    Initialize a package.json file with the following command:
    1
    npm init
    Copied!
    2.
    Install Hapi.js:
    1
    npm install --save @hapi/hapi
    Copied!

Create

Create a server.js file with the following contents:
1
"use strict";
2
​
3
const Hapi = require("@hapi/hapi");
4
​
5
const init = async () => {
6
const server = Hapi.server({
7
port: process.env.PORT || 8080,
8
host: "0.0.0.0"
9
});
10
​
11
server.route({
12
method: "GET",
13
path: "/",
14
handler: (request, h) => {
15
return "Hello World!";
16
}
17
});
18
​
19
await server.start();
20
console.log("Server running on %s", server.info.uri);
21
};
22
​
23
process.on("unhandledRejection", err => {
24
console.log(err);
25
process.exit(1);
26
});
27
​
28
init();
Copied!

Run

    1.
    Run the app with the following command:
    1
    npm start
    Copied!
    2.
    Visit http://localhost:8080 to see the Hello World! message.

Deploy

    1.
    Create an app.yaml file with the following contents:
    1
    runtime: nodejs
    2
    env: flex
    Copied!
    2.
    Deploy your app:
    1
    gcloud app deploy
    Copied!
    3.
    Visit http://YOUR_PROJECT_ID.appspot.com to see the Hello World! message.
Last modified 7mo ago