Created
          March 17, 2014 00:49 
        
      - 
      
 - 
        
Save anonymous/9592099 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | Here is the console log after starting the app and posting data to it's /receiveHit listener. | |
| The result is a successful new entry into the database. | |
| Application initializing... | |
| Application successfully initialized! | |
| Server running at http://127.0.0.1:8080/ | |
| User username1 is attempting to validate for a hit... | |
| Connection with the Officeball MySQL database openned... | |
| ...Connection with the Officeball MySQL database closed. | |
| User username1 validated and is ready! | |
| [Post] Connection with the officeball MySQL database opened... | |
| // it ends there! I would expect "[Post] ...Connection with the Officeball MySQL database closed." and | |
| // failing that, a fatal error. | |
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | console.log("Application initializing..."); | |
| // REQUIRE ALL MODULES | |
| var mysql = require('mysql'); | |
| var express = require('express'); | |
| //DECLARE ALL GLOBAL VARIABLES | |
| var username; | |
| var password; | |
| var app = express(); | |
| //DO WEIRD THINGS | |
| Date.prototype.yyyymmdd = function () { | |
| var yyyy = this.getFullYear().toString(); | |
| var mm = (this.getMonth() + 1).toString(); // getMonth() is zero-based | |
| var dd = this.getDate().toString(); | |
| return yyyy + '-' + (mm[1] ? mm : "0" + mm[0]) + '-' + (dd[1] ? dd : "0" + dd[0]); | |
| }; | |
| d = new Date(); | |
| //DEFINE ALL FUNCTIONS | |
| function createConnection() { | |
| var connection = mysql.createConnection({ | |
| host: 'localhost', | |
| user: 'root', | |
| password: '', | |
| database: 'officeball' | |
| }); | |
| return connection; | |
| } | |
| function loginSucceed(req, res, result) { | |
| loginData = JSON.stringify(result); | |
| res.writeHead(200, { | |
| 'Access-Control-Allow-Origin': 'http://localhost', | |
| 'Content-Length': loginData.length, | |
| 'Content-Type': 'application/json' | |
| }); | |
| res.write(loginData); | |
| console.log('User ' + req.body.username + ' logged in successfully.'); | |
| res.end(); | |
| } | |
| function loginFail(req, res, err) { | |
| if (err) { | |
| var body = 'Oops! There was an error while processing your request.'; | |
| res.writeHead(200, { | |
| 'Access-Control-Allow-Origin': 'http://localhost', | |
| 'Content-Length': body.length, | |
| 'Content-Type': 'text/plain' | |
| }); | |
| console.log('User ' + req.body.username + ' triggered error: ' + err); | |
| body = JSON.stringify(body); | |
| res.write(body); | |
| } else { | |
| var body = 'Username and password didn\'t match.'; | |
| res.writeHead(200, { | |
| 'Access-Control-Allow-Origin': 'http://localhost', | |
| 'Content-Length': body.length, | |
| 'Content-Type': 'text/plain' | |
| }); | |
| console.log('User ' + req.body.username + ' failed to validate.'); | |
| body = JSON.stringify(body); | |
| res.write(body); | |
| } | |
| res.end(); | |
| } | |
| function missServer(req, res, err) { | |
| console.log('User ' + req.body.username + ' failed to validate!'); | |
| var body = 'Failed to post data!'; | |
| body = JSON.stringify(body); | |
| res.writeHead(200, { | |
| 'Access-Control-Allow-Origin': 'http://localhost', | |
| 'Content-Length': body.length, | |
| 'Content-Type': 'text/plain' | |
| }); | |
| res.write(body); | |
| res.end(); | |
| } | |
| function hitServer(req, res, result) { | |
| console.log('User ' + req.body.username + ' validated and is ready!'); | |
| postSale(req, res, result); | |
| } | |
| function listen() { | |
| app.use(express.bodyParser()); | |
| app.post('/login', function (req, res) { | |
| var username = req.body.username; | |
| var password = req.body.password; | |
| console.log('User ' + username + ' is attempting login...'); | |
| validate(username, password, function (err, result) { | |
| if (err) loginFail(req, res, err); | |
| else loginSucceed(req, res, result); | |
| }); | |
| }); | |
| app.post('/recieveHit', function (req, res) { | |
| var username = req.body.username; | |
| var password = req.body.password; | |
| console.log('User ' + username + ' is attempting to validate for a hit...'); | |
| validate(username, password, function (err, result) { | |
| if (err) missServer(req, res, err); | |
| else hitServer(req, res, result); | |
| }); | |
| }); | |
| app.listen(8080, function () { | |
| console.log('Server running at http://127.0.0.1:8080/'); | |
| }); | |
| } | |
| function validate(username, password, callback) { | |
| var connection = createConnection(); | |
| connection.connect(function (err) { | |
| if (err) return callback(new Error('Failed to connect'), null); | |
| console.log('Connection with the Officeball MySQL database openned...'); | |
| connection.query('select username,password,fname,lname,rank,active from users where username=?', | |
| username, | |
| function (err, rows, fields) { | |
| connection.destroy(); | |
| console.log('...Connection with the Officeball MySQL database closed.'); | |
| if (err) return callback(new Error('Error while performing query'), null); | |
| if (rows.length !== 1) return callback(new Error('- [Anomaly] - Failed to find exactly one user'), null); | |
| if (rows[0].password === password & rows[0].active === "yes") { | |
| var result = new Object(); | |
| result.username = rows[0].username; | |
| result.password = rows[0].password; | |
| result.fname = rows[0].fname; | |
| result.lname = rows[0].lname; | |
| result.rank = rows[0].rank; | |
| return callback(null, result); | |
| } | |
| if (rows[0].active !== "yes") { | |
| return callback(new Error('User account not active.'), null); | |
| } else { | |
| return callback(new Error('Login credentials did not match.'), null); | |
| } | |
| }); | |
| }); | |
| } | |
| function postSale(req, res, result, callback) { | |
| var name = req.body.fname + ' ' + req.body.lname; | |
| var group = req.body.group; | |
| //var category = groupToCat(group); | |
| var category = 'fire'; | |
| var customer = req.body.customer; | |
| var price = req.body.price; | |
| var date = d.yyyymmdd(); | |
| console.log(name + ' is attempting to sell a ' + category + ' line to ' + customer + ' for ' + price + ' on the date ' + date + '.'); | |
| var commission = ''; | |
| var salesData = [category, group, date, price, customer, name, commission]; | |
| var connection = createConnection(); | |
| connection.connect(function (err) { | |
| if (err) return callback(new Error('Failed to connect'), null); | |
| console.log('Connection with the officeball MySQL database openned...'); | |
| // if no error, you can do things now. | |
| connection.query( | |
| 'INSERT INTO `officeball`.`sales_entries` SET category = ?, `group` = ?, date = ?, price = ?, customer = ?, seller = ?, commission = ?', | |
| salesData), | |
| function (err, rows, fields) { | |
| connection.destroy(); | |
| console.log('...Connection with the officeball MySQL database closed.'); | |
| if (err) { | |
| console.log(err); | |
| return callback(new Error('Failed to post sales entry'), null); | |
| } else { | |
| res.writeHead(200, { | |
| 'Access-Control-Allow-Origin': 'http://localhost', | |
| 'Content-Length': body.length, | |
| 'Content-Type': 'text/plain' | |
| }); | |
| var body = 'Posted sales entry successfully!'; | |
| body = JSON.stringify(body); | |
| res.write(body); | |
| res.end(); | |
| } | |
| } | |
| }); | |
| } | |
| //INITALIZE FUNCTIONS | |
| listen(); | |
| //SAY NICE THINGS | |
| console.log("Application successfully initialized!"); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment