Building modern web applications-Part 2

Nginx reverse proxy has been configured and service is running after the successful configuration of nginx.You might get a “Bad Gateway error “ .if we try to connect to nginx using web browser.This means packets are hitting nginx reverse proxy file ,but since our backend server is not initialized and started yet it is giving a gateway error.

Checkpoint 1: Nginx web server configured

Configuring app server in google cloud

Here we will configure a node.js application as an app server for our testing.The application is simple and it will be listening on port 7555 for any incoming connections and will show a dialog box to create a new user, And when we click on “Create User”,a new user will be created in the mysql server which we will be using as a backend service.

Node.js app-****(this part has been taken from the link
Thanks to Robert Tod

Creating and initializing node.js app

Install Node.js
Install MySQL
Create a HTTP API for writing to the database
Create some HTML and JS to POST to the API
Use Knex migrations to create a user database schema

root@web01:~/tutorial_node_database# ls
index.js knexfile.js migrations node_modules package.json public store.js
root@web01:~/tutorial_node_database# cat index.js
const express = require(‘express’)
const bodyParser = require(‘body-parser’)
const store = require(‘./store’)
const app = express()
app.use(bodyParser.json())‘/createUser’, (req, res) => {
username: req.body.username,
password: req.body.password
.then(() => res.sendStatus(200))
app.listen(7555, () => {
console.log(‘Server running on http://localhost:7555’)

root@web01:~/tutorial_node_database# cat knexfile.js
module.exports = {
client: ‘mysql’,
connection: {
user: ‘root’,
password: ‘sree’,
database: ‘tutorial_node_database’
root@web01:~/tutorial_node_database# cat package.json
“name”: “tutorial_node_database”,
“version”: “1.0.0”,
“description”: “”,
“main”: “index.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1″
“author”: “”,
“license”: “ISC”,
“dependencies”: {
“body-parser”: “^1.18.1”,
“express”: “^4.15.4”,
“knex”: “^0.13.0”,
“mysql”: “^2.14.1”
root@web01:~/tutorial_node_database# cat store.js
const knex = require(‘knex’)(require(‘./knexfile’))
module.exports = {
createUser ({ username, password }) {
console.log(`Add user ${username} with password ${password}`)
return knex(‘users’).insert({

root@web01:~/tutorial_node_database# cd public/
root@web01:~/tutorial_node_database/public# ls
app.js index.html
root@web01:~/tutorial_node_database/public# cat app.js
const CreateUser = document.querySelector(‘.CreateUser’)
CreateUser.addEventListener(‘submit’, (e) => {
const username = CreateUser.querySelector(‘.username’).value
const password = CreateUser.querySelector(‘.password’).value
post(‘/createUser’, { username, password })
function post (path, data) {
return window.fetch(path, {
method: ‘POST’,
headers: {
‘Accept’: ‘application/json’,
‘Content-Type’: ‘application/json’
body: JSON.stringify(data)

root@web01:~/tutorial_node_database/public# cat index.html
<!DOCTYPE html>
<title>Node database tutorial</title>
<form class=”CreateUser”>
<h1>Create a new user</h1>
<input type=”text” class=”username” placeholder=”username”>
<input type=”password” class=”password” placeholder=”password”>
<input type=”submit” value=”Create user”>
<script src=”/app.js”></script>

Configure the knexfile.js appropriately to connect to the database.
Use knex to create a new user

root@web01:~/tutorial_node_database# knex migrate:make new_user_for_node
Created Migration: /root/tutorial_node_database/migrations/20170924060137_new_user_for_node.js

Copy the below contents

exports.up = function (knex) {
return knex.schema.createTable(‘user’, function (t) {
t.timestamps(false, true)
exports.down = function (knex) {
return knex.schema.dropTableIfExists(‘user’)

Move to working directory and start node

root@e253b80241fc:/tutorial-node-database# node .
Server running on http://localhost:7555


Installing and configuring mysql server.
Create an instance in google cloud.

root@web01:/# sudo apt-get install mysql-server
root@web01:/# service mysql restart

We have configured three instances and the application will be accessible through nginx webserver
Testing the application
We can see from the live node console that user got added

root@e253b80241fc:/tutorial-node-database# node .
Server running on http://localhost:7555
Add user sree6 with password sree6

We check the DB server we can see the user got added.

mysql> show databases;
| Database |
| information_schema |
| mysql |
| performance_schema |
| sys |
| tutorial_node_database |
5 rows in set (0.04 sec)
mysql> use tutorial_node_database
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from user;
| id | username | password | created_at | updated_at |
| 1 | sree6 | sree6 | 2017-09-24 11:51:37 | 2017-09-24 11:51:37 |
1 row in set (0.00 sec)
mysql> exit

Posted in Uncategorized.

Leave a Reply

Your email address will not be published. Required fields are marked *