手順
- nodeのversionを8にする
- 「Google Cloud SDK」をinstall
- Expressで「Hello, world!」表示
nodeのversion設定
nodeのversionを「8.11.4」に設定
※App Engineのnodeのversionが「8」のため
$ 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で実行
.bash_profile
source /usr/local/var/google-cloud-sdk/completion.bash.inc
source /usr/local/var/google-cloud-sdk/path.bash.inc
gcloudのversionを確認
$ 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を通す
.bash_profile
export PATH=/usr/local/var/google-cloud-sdk/bin/dev_appserver.py:$PATH
Expressで「「Hello, world!」」表示
「Google App Engine Node.js Samples」を参考
適当な場所でディレクトリを作成後、Goolgleアカウントで認証
$ gcloud auth application-default login
参考のリポジトリをclone
$ git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
「sample-folder」を作成
nodejs-docs-samples/appengine
$ mkdir sample-folder
npm initでpackage.json作成
sample-folder
$ npm init -y
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
}
}
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]
$ npm start
http://localhost:8080/で「Hello, world!」が表示されればOK!