forked from paulnguyen/heroku-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
122 lines (99 loc) · 3.78 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
Mighty Gumball, Inc.
Version 1.0
- Rudimentary Page Templates using RegEx
- REST Client Calling Go REST API
NodeJS-Enabled Standing Gumball
Model# M102988
Serial# 1234998871109
**/
var machine = "https://pnguyen-goapi.herokuapp.com/gumball";
var endpoint = "https://pnguyen-goapi.herokuapp.com/order";
var fs = require('fs');
var express = require('express');
var connect = require('connect');
var Client = require('node-rest-client').Client;
var app = express();
//var connect = require('connect');
app.use(connect.cookieParser())
app.use(express.urlencoded());
app.use(express.json());
app.use("/images", express.static(__dirname + '/images'));
var page = function( req, res, state ) {
var body = fs.readFileSync('./gumball.html');
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
var client = new Client();
var count = "";
client.get( machine,
function(data, response_raw){
console.log(data);
jsdata = JSON.parse(data)
for(var key in jsdata) {
console.log( "key:" + key + ", value:" + jsdata[key] );
}
count = jsdata.CountGumballs
console.log( "count = " + count ) ;
var msg = "\n\nMighty Gumball, Inc.\n\nNodeJS-Enabled Standing Gumball\nModel# " +
jsdata.ModelNumber + "\n" +
"Serial# " + jsdata.SerialNumber + "\n" +
"State: " + state + "\n" +
"Inventory: " + count + "\n" ;
var html_body = "" + body ;
var html_body = html_body.replace("{message}", msg );
var html_body = html_body.replace(/id="state".*value=".*"/, "id=\"state\" value=\""+state+"\"") ;
res.end( html_body );
});
}
var order = function(req, res) {
var client = new Client();
var count = 0;
client.get( machine,
function(data, response_raw) {
jsdata = JSON.parse(data)
count = jsdata.CountGumballs ;
console.log( "count before = " + count ) ;
if ( count > 0 )
count-- ;
console.log( "count after = " + count ) ;
var args = {
data: { "CountGumballs": count },
headers:{"Content-Type": "application/json"}
};
client.put( machine, args,
function(data, response_raw) {
console.log(data);
page( req, res, "no-coin" ) ;
}
);
});
}
var handle_post = function (req, res) {
console.log( "Post: " + "Action: " + req.body.event + " State: " + req.body.state + "\n" ) ;
var state = "" + req.body.state ;
var action = "" + req.body.event ;
if ( action == "Insert Quarter" ) {
if ( state == "no-coin" )
page( req, res, "has-coin" ) ;
else
page( req, res, state ) ;
}
else if ( action == "Turn Crank" ) {
if ( state == "has-coin" ) {
order(req, res) ;
}
else
page( req, res, state ) ;
}
}
var handle_get = function (req, res) {
console.log( "Get: ..." ) ;
page( req, res, "no-coin" ) ;
}
app.set('port', (process.env.PORT || 8080));
app.post("*", handle_post );
app.get( "*", handle_get ) ;
console.log( "Server running on Port 8080..." ) ;
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});