Fast and Frugal App Delivery With an Integrated Production and Development Paradigm

Amir Ghorbanian
Mar 4 · 8 min read
Photo by Kelvin Ang on Unsplash

Contents

Introduction

Pros and Cons of an Integrated Production and Development Setup

Prerequisites

Step 1 — Building a Mock Node application

$ mkdir ~/node_application$ cd ~/node_application$ npm init -y$ touch index.js
const express = require('express');const app = express();const PORT = process.env.PORTapp.get('/',(req,res) => {
res.send("Hello World!\n");
})
app.listen(PORT,() => {
console.log(`server is running on port ${PORT}`);
})
npm install express
$ pm2 ecosystem$ PORT=8080 pm2 start ecosystem.config.js 
$ curl localhost:8080
Hello World!

Step 2 — Setting up a Production and Development Branch

$ cd ~$ cp -r ~/node_application ./production
$ mv ~/node_application ~/development
...app.get('/',(req,res) => {
res.send("Hello World, I am the development version!\n");
})
...
module.exports = {
apps : [{
script: 'index.js',
watch: '.',
// give your process a meaningful name
name: 'development'
}, {
script: './service-worker/',
watch: ['./service-worker']
}], deploy : {
production : {
user : 'SSH_USERNAME',
host : 'SSH_HOSTMACHINE',
ref : 'origin/master',
repo : 'GIT_REPOSITORY',
path : 'DESTINATION_PATH',
'pre-deploy-local': '',
'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production',
'pre-setup': ''
}
}
};
$ cd ~/development$ PORT=8081 pm2 start ecosystem.config.js$ cd ~/production$ PORT=8080 pm2 start ecosystem.config.js
$ curl localhost:8081
Hello World, I am the development version!
$ curl localhost:8080
Hello World!

Step 3— Setting up Isolated Production and Development Databases

$ sudo -u postgres createdb production $ sudo -u postgres createdb development
$ cd ~/development$ npm i pg
$ sudo -u postgres psql development
development=# CREATE TABLE users (
development(# name CHAR(50)
development(# );
const express = require('express');// Import package for interfacing with PostgreSQL
const { Client } = require('pg');
// Configure client
const client = new Client({
user: 'postgres',
host: 'localhost',
database: 'development',
password: 'password',
port: '5432'
})
// Connect to server
client.connect(err => {
if (err) console.log(err.stack)
})
const app = express();
const PORT = process.env.PORT
app.get('/',(req,res) => {
res.send("Hello World, I am the development version!\n");
})
// Create a route to test adding data to database
app.get('/test-pg/users/:name', (req, res) => {
let name = req.params.name
client.query('INSERT INTO users(name) VALUES($1) RETURNING *', [name], (err, result) => {
if (err) {
res.status(500).send(err.stack)
} else {
res.status(200).send(result.rows[0])
}
})
})
app.listen(PORT,() => {
console.log(`server is running on port ${PORT}`);
})
$ curl localhost:8081/test-pg/users/Bob
{"name":"Bob"}

Conclusion

Geek Culture

Proud to geek out.

Sign up for Geek Culture Hits

By Geek Culture

Subscribe to receive top 10 most read stories of Geek Culture — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Amir Ghorbanian

Written by

Software engineer and FOSS enthusiast | LinkedIn: https://www.linkedin.com/in/amir-ghorbanian

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store