I tried to autoscale Web server with Node.js using PaaS 'App Engine' of Google Cloud


" Google App Engine " provided by Google is a cloud service classified as PaaS (Platform as a Service), and it is possible to install an application that scales according to the number of accesses without doing any infrastructure setting such as a server Service. I thought that the standard environment of this Google App Engine corresponded to Node.js in June, 2018, and I immediately tried installing a Web server using Node.js. Although we are using macOS this time, you can install the server in the same procedure on Windows.

App Engine - Build Scalable Web & Mobile Backends in Any Language | App Engine | Google Cloud
https://cloud.google.com/appengine/

Firstly, if Node.js is not installed, download the LTS version from the official site and install it.

Node.js
https://nodejs.org/en/



We prepare an editor for programming. Any editor is fine, but this time I will use " Visual Studio Code ". You can download by clicking the green button on the left side of the screen.



Execute the downloaded file, install Visual Studio Code, and it will become the screen below when it starts up. Click "Add Workspace Folder".



Create a folder named "nodeWebServer" and click "Add".



Since the folder is added to the "workspace" of "Explorer" at the top of the left column, click "Open in Terminal" from the menu displayed by right-clicking.



Then "Terminal" is displayed in the lower right. Make sure that the terminal has "nodeWebServer" open

[code] npm init - y [/ code]

Enter. This "npm" is a package management tool for node.js installed together with node.js. When you enter the command "npm init" you can make npm initialize ( init ialize) and generate "package.json" which is package management code. Originally I will write an explanation of what kind of code is interactive, but with the "- y" option all package.json can be generated with default settings.



Next, install "express" of the package which makes the setup of the server super easy.

[code] npm install express [/ code]

, A folder called "node_modules" is created under nodeWebServer, and express and the library for moving it are installed there. In addition, "package - lock.json" generated at the same time is a code for recording the version of the library, and if you share this code with others, you can pass this package - lock.json and if you use which version of the library Whether it is good or not can be understood with a single shot.



Next I will write the code of the server itself. Create a file called "app.js" in the "nodeWebServer" folder.



With reference to Express' s Hello World code , I wrote the following code in app.js.

[code] / **
* express Import the library.
* /
var express = require ("express");


/ **
* Instantiate it and assign it to app.
* /
var app = express ();


/ **
* Write reply when HTTP request method is 'get'.
* /
app.get ('/', function (req, res) {

res.send ('Hello World!');

});


/ **
* We will listen on port 3000.
* /
app.listen (3000, function () {

console.log ('App listening on port 3000');

});
[/ code]


After entering the code in app.js,

[code] node app.js [/ code]

And run app.js on node.js.



When you visit http: // localhost: 3000 / it is safe, "Hello World!" is displayed. To terminate a running application, enter "Ctrl + C" on the terminal.


This time do not enter the detailed specification of express, try deploying an application displaying this simple "Hello World!" On Google App Engine. In order to use Google App Engine, you need to register with Google Cloud Platform which is Google cloud service, so if you are not registered yet, please register by referring to the beginning of the following article.

How to multiply multiple clients with a Minecraft server on Google's cloud for free - GIGAZINE



Next, install the tool " Cloud Tools " for managing Google Cloud Platform. Select your OS, download "Cloud SDK installer" and execute it.



Once I can install Cloud Tools, I will finally set up to deploy the application on Google App Engine. First of all, it is about app.js created earlier, but since it is not clearly decided which port number should be used when running on App Engine, there is information "What kind of environment is working?" Rewrite to use environment variable "process.env". By doing this, the server that runs on App Engine can listen on the proper port and it will be able to access properly.

[code] / **
* We will listen on port 3000.
* /
app.listen (3000, function () {

console.log ('App listening on port 3000');

}); [/ code]

↓ ↓ ↓

[code] / **
* If the environment variable "PORT" is set, its number,
* If not, it will listen on port 3000.
* /
app.listen (process.env.PORT || 3000, function () {

console.log ('App listening on port 3000');

}); [/ code]


Next, we will create "app.yaml" which is a setting file of how to operate on Google App Engine. Create "app.yaml" in the same folder as app.js and add it as follows.

[code] runtime: nodejs8 [/ code]


By installing Cloud Tools you will be able to use the command "gcloud", so at the terminal

[code] gcloud app deploy [/ code]

And press the Enter key. The following confirmation dialog will appear, so please check and enter "y" to deploy the application.



When deployment is complete,

[code] gcloud app browse [/ code]

If you enter, the deployment destination page will be displayed. If it says "Hello World!" It was successful, but somehow an error has been displayed.



You can see what errors are occurring inside App Engine at the bottom of App Engine's dashboard.



The error message is as follows. Usually I just need to search this error message on Google, but I did not hit anything this time, so I will do it on my own.

[code] Error: Can not find module '/srv/server.js'
Function.Module._resolveFilename (module.js) [/ code]


Apparently, I tried various experiments and it seemed that the "scripts" part of package.json,

[code] "scripts": {

"test": "echo \" Error: no test specified \ "&& exit 1"
}, [/ code]

It seems necessary to rewrite from

[code] "scripts": {

"start": "node app.js"
}, [/ code]


After correcting the scripts of package.json, return it to the terminal again

[code] gcloud app deploy [/ code]

Enter and redeploy. Earlier

[code] gcloud app browse [/ code]

"Hello World!" Was displayed properly when reloading the page which was open at.



In this way, servers deployed on Google App Engine are scaled automatically according to the number of accesses, and a fee will be charged according to the scaling. From the 'Settings' tab on the left to prevent you from being charged a lot of money when you make a mistake on installation ......



It is OK if you set "upper limit of usage per day".

in Software,   Web Service,   Web Application, Posted by log1d_ts