Google Cloud Platform

We can deploy on a real-world hosting server, such as Google Cloud.

 

A Google Cloud deployment will automatically set the environment variable process.env.NODE_ENV to "production".

We can use this to check if we are running on a Google Cloud server, as shown below:

if(process.env.NODE_ENV === "production") // gcloud production mode
{

    ...

}
else // development mode
{

    ...

}

 

The server.js file must be changed so that it allows a Google Cloud hosted server to run, as shown below:

server/server.js

const express = require('express')
const app = express()

app.use(require(`body-parser`).json())

let PORT
if(process.env.NODE_ENV === "production") // gcloud production mode
{
    PORT = process.env.PORT    // Port is automatically set by gcloud and stored in the environment variable "process.env.PORT"
}
else // development mode
{
    console.log("Development mode. Running on local host server")
    require(`dotenv`).config({path:`./config/.env`})     // Cannot have .env in gcloud
    app.use(require(`cors`)({credentials: true, origin: process.env.LOCAL_HOST}))  //  not needed in gcloud PORT = process.env.SERVER_PORT
}



const path = require("path")
const appPath = path.join(__dirname,"..","client","build")
app.use(express.static(appPath))

app.get('/', (req, res) => 
{
    res.sendFile(path.resolve(appPath, "index.html"))
})


// Routers
app.use(require(`./routes/cars`))


// Port
app.listen(PORT, () => 
{
    console.log(`Connected to port  ${PORT}`)
})


// Error 404
app.use((req, res, next) => {next(createError(404))})

// Other errors
app.use(function (err, req, res, next)
{
    console.error(err.message)
    if (!err.statusCode) 
    {
        err.statusCode = 503
    }
    res.status(err.statusCode).send(err.message)
})

 

 

The client-side SERVER_HOST constant must be changed to point to the Google Cloud server, as shown below:

client/src/config/global_constants.js

// This file holds global constants that are visible on the Client-side


// Server
//export const SERVER_HOST = `http://localhost:4000`
export const SERVER_HOST = `https://dkit-full-stack.nw.r.appspot.com`

 

To host a nodejs app on Google Cloud, we must set up an account at https://console.cloud.google.com/

 

 

 

 

Google Cloud requires file called app.yaml to be placed in the root folder of the project. The minimum content that must be included in the app.yaml file is:

runtime: nodejs16

 

To initialise gcloud, run the following command

gcloud init

You will be prompted to complete the login settings for your account.

 

 

To deploy a production version of your app onto Google, you need to go to the root folder of your app and run the command below:

gcloud app deploy

The gcloud app deploy command will give you the address of your project in Google Cloud. In the example above, the app has been deployed to https://dkit-full-stack.nw.r.appspot.com

 

 

The Google Cloud app can also be accessed via the command line, using the code below:

gcloud app browse

Set up a Google Cloud nodejs project.

Initialise Google Cloud.

Amend the code from the previous deployment server section of the notes (here) so that it can be deployed on Google Cloud.

"Cars" Worked Example

The full project code for the "Cars" Worked Example that is described below can be downloaded from this link.

Root Folder

/app.yaml

runtime: nodejs16

Client-Side

client/src/config/global_constants.js

// This file holds global constants that are visible on the Client-side


// Server
//export const SERVER_HOST = `http://localhost:4000`
export const SERVER_HOST = `https://dkit-full-stack.nw.r.appspot.com`

Server-Side

server/server.js

const express = require('express')
const app = express()

app.use(require(`body-parser`).json())

let PORT
if(process.env.NODE_ENV === "production") // gcloud production mode
{
    PORT = process.env.PORT    // Port is automatically set by gcloud and stored in the environment variable "process.env.PORT"
}
else // development mode
{
    console.log("Development mode. Running on local host server")
    require(`dotenv`).config({path:`./config/.env`})     // Cannot have .env in gcloud
    app.use(require(`cors`)({credentials: true, origin: process.env.LOCAL_HOST}))  //  not needed in gcloud PORT = process.env.SERVER_PORT
}



const path = require("path")
const appPath = path.join(__dirname,"..","client","build")
app.use(express.static(appPath))

app.get('/', (req, res) => 
{
    res.sendFile(path.resolve(appPath, "index.html"))
})


// Routers
app.use(require(`./routes/cars`))


// Port
app.listen(PORT, () => 
{
    console.log(`Connected to port  ${PORT}`)
})


// Error 404
app.use((req, res, next) => {next(createError(404))})

// Other errors
app.use(function (err, req, res, next)
{
    console.error(err.message)
    if (!err.statusCode) 
    {
        err.statusCode = 503
    }
    res.status(err.statusCode).send(err.message)
})

 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>