「Google App Engine」でNode.jsを使用するために「Hello World」表示

## 手順

- nodeのversionを8にする
- 「Google Cloud SDK」をinstall
- Expressで「Hello, world!」表示

## nodeのversion設定

nodeのversionを「8.11.4」に設定
※App Engineのnodeのversionが「8」のため

```terminal
$ nvm install v8.11.4
$ nvm use v8.11.4
$ node -v
v8.11.4
```

## 「Google Cloud SDK」をinstall

localからGCPにアクセスするために「Google Cloud SDK」をinstall
https://cloud.google.com/sdk/

### Python 2.7がインストールされていることを確認

```
$ python -V
Python 2.7.13
```

### SDKをinstall

Mac OS X（x86_64）の場合
google-cloud-sdk-180.0.0-darwin-x86_64.tar.gz

Mac OS X（x86）の場合
google-cloud-sdk-180.0.0-darwin-x86.tar.gz

上記アーカイブを解凍してinstall

```
$ sh ./google-cloud-sdk/install.sh
```

.bash_profileで実行

```terminal:.bash_profile
source /usr/local/var/google-cloud-sdk/completion.bash.inc
source /usr/local/var/google-cloud-sdk/path.bash.inc
```

gcloudのversionを確認

```terminal
$ source ~/.bash_profile
$ gcloud -v
Google Cloud SDK 180.0.0
bq 2.0.27
core 2017.11.10
gsutil 4.28
```

dev_appserver.pyにPATHを通す

```terminal:.bash_profile
export PATH=/usr/local/var/google-cloud-sdk/bin/dev_appserver.py:$PATH
```

## Expressで「「Hello, world!」」表示

「[Google App Engine Node.js Samples](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/README.md)」を参考

適当な場所でディレクトリを作成後、Goolgleアカウントで認証

```terminal
$ gcloud auth application-default login
```

参考のリポジトリをclone

``` terminal
$ git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
```

「sample-folder」を作成

``` terminal:nodejs-docs-samples/appengine
$ mkdir sample-folder
```

npm initでpackage.json作成

``` terminal:sample-folder
$ npm init -y
```

``` json:package.json
{
  "name": "appengine-hello-world",
  "description": "Simple Hello World Node.js sample for Google App Engine Standard Environment.",
  "version": "0.0.1",
  "private": true,
  "license": "Apache-2.0",
  "author": "Google Inc.",
  "repository": {
    "type": "git",
    "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
  },
  "engines": {
    "node": "8.x.x"
  },
  "scripts": {
    "deploy": "gcloud app deploy",
    "start": "node app.js",
    "lint": "repo-tools lint",
    "pretest": "npm run lint",
    "system-test": "repo-tools test app",
    "test": "npm run system-test",
    "e2e-test": "repo-tools test deploy"
  },
  "dependencies": {
    "express": "^4.16.3"
  },
  "devDependencies": {
    "@google-cloud/nodejs-repo-tools": "^2.3.0",
    "semistandard": "^12.0.1"
  },
  "cloud-repo-tools": {
    "test": {
      "app": {
        "msg": "Hello, world!"
      }
    },
    "requiresKeyFile": true,
    "requiresProjectId": true
  }
}
```

```javascript:app.js
/**
 * Copyright 2017, Google, Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

// [START gae_node_request_example]
const express = require('express');

const app = express();

app.get('/', (req, res) => {
  res.status(200).send('Hello, world!').end();
});

// Start the server
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`App listening on port ${PORT}`);
  console.log('Press Ctrl+C to quit.');
});
// [END gae_node_request_example]
```

``` terminal
$ npm start
```

[http://localhost:8080/](http://localhost:8080/)で「Hello, world!」が表示されればOK!


## 参考サイト

- [Google App Engine Node.js Samples](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/README.md)
- [Mac OS X 用のクイックスタート](https://cloud.google.com/sdk/docs/quickstart-mac-os-x)
- [Cloud SDKをmac, Linux, Windowsにインストール・初期設定までの導入手順徹底解説！！](https://www.apps-gcp.com/cloud-sdkinstall-commentary/)
- [.bash_profile ? .bashrc ? いろいろあるけどこいつらなにもの？](https://qiita.com/hirokishirai/items/5a529c8395c4b336bf31)
- [nvmを使ったNode.jsのインストール&バージョンアップ手順](https://qiita.com/ffggss/items/94f1c4c5d311db2ec71a)
- [Quickstart for Node.js in the App Engine standard environment](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/appengine/hello-world/standard)