├── README.md ├── postgres ├── Dockerfile └── seed.sql ├── Dockerfile ├── create_image.sh ├── package.json └── src ├── views └── pages │ └── index.ejs └── server.js /README.md: -------------------------------------------------------------------------------- 1 | # WatchUsBuild-SimpleNodeAppWithDocker 2 | Watch Us Build code for the Simple Node App With Docker screencast. 3 | -------------------------------------------------------------------------------- /postgres/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM postgres:9.6 2 | 3 | LABEL maintainer aj-foster@pluralsight.com 4 | 5 | EXPOSE 5432 6 | 7 | COPY seed.sql /docker-entrypoint-initdb.d/ -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:4-onbuild 2 | 3 | LABEL maintainer jon-friskics@pluralsight.com 4 | 5 | EXPOSE 8888 6 | 7 | RUN mkdir -p /usr/src/app 8 | WORKDIR /usr/src/app 9 | 10 | COPY package.json /usr/src/app/ 11 | RUN npm install 12 | 13 | CMD ["npm", "start"] -------------------------------------------------------------------------------- /postgres/seed.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE votes ( 2 | id integer PRIMARY KEY, 3 | option_name varchar(20), 4 | number_of_votes integer 5 | ); 6 | 7 | INSERT INTO votes (id, option_name, number_of_votes) VALUES (1, 'tacos', 10); 8 | INSERT INTO votes (id, option_name, number_of_votes) VALUES (2, 'sandwiches', 7); -------------------------------------------------------------------------------- /create_image.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # remove any existing containers 4 | docker container prune -f 5 | 6 | # build the image and run the container 7 | docker build -t voting-app . 8 | docker container run -it -p 8888:8888 -v ~/Desktop/DOCKER/wub1/WatchUsBuild-SimpleNodeAppWithDocker/src:/usr/src/app/src voting-app -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "voting_app", 3 | "version": "1.0.0", 4 | "description": "A Simple Voting App", 5 | "author": "Jon Friskics ", 6 | "main": "src/server.js", 7 | "scripts": { 8 | "start": "nodemon src/server.js" 9 | }, 10 | "dependencies": { 11 | "nodemon": "^1.11.0", 12 | "express": "^4.13.3", 13 | "body-parser": "^1.16.1", 14 | "ejs": "^2.5.6", 15 | "pg": "^6.1.2" 16 | } 17 | } -------------------------------------------------------------------------------- /src/views/pages/index.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | Voting App 4 | 5 | 6 |

Vote Now!

7 |
8 |
9 | 10 | 11 |
12 |
13 | 14 | 15 |
16 | 17 |
18 | 22 | 23 | -------------------------------------------------------------------------------- /src/server.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const express = require('express'); 4 | const app = express(); 5 | let bodyParser = require('body-parser'); 6 | let ejs = require('ejs'); 7 | let pg = require('pg'); 8 | 9 | let client = new pg.Client('postgres://postgres:blahblah@172.17.0.1:9000/postgres'); 10 | 11 | let votes = { 12 | sandwiches: 0, 13 | tacos: 0 14 | }; 15 | 16 | client.connect(function (err) { 17 | if (err) throw err; 18 | client.query('SELECT number_of_votes FROM votes', function (err, result) { 19 | if (err) throw err; 20 | 21 | votes.sandwiches = result.rows[0].number_of_votes; 22 | votes.tacos = result.rows[1].number_of_votes; 23 | }); 24 | }); 25 | 26 | let urlencodedParser = bodyParser.urlencoded({ extended: false }) 27 | 28 | app.set('view engine', 'ejs'); 29 | app.set('views', __dirname + '/views') 30 | 31 | app.get('/', function (req, res) { 32 | res.render('pages/index', { 33 | votes: votes 34 | }); 35 | }); 36 | 37 | app.post('/vote', urlencodedParser, function(req, res) { 38 | let vote = req.body.yourVote; 39 | if(vote === 'sandwiches') { 40 | votes.sandwiches = votes.sandwiches + 1; 41 | client.query('UPDATE votes SET number_of_votes=\'' + votes.sandwiches + '\' WHERE option_name=\'sandwiches\'', function (err, result) { 42 | if (err) throw err; 43 | }); 44 | } else if(vote === 'tacos') { 45 | votes.tacos = votes.tacos + 1; 46 | client.query('UPDATE votes SET number_of_votes=\'' + votes.tacos + '\' WHERE option_name=\'tacos\'', function (err, result) { 47 | if (err) throw err; 48 | }); 49 | } else { 50 | console.log('Something went wrong: vote contains ' + vote); 51 | } 52 | res.redirect('/'); 53 | }); 54 | 55 | const PORT = 8888; 56 | app.listen(PORT); 57 | console.log('Running on http://localhost:' + PORT); --------------------------------------------------------------------------------