NodeJS, MySQL beacon app

A future post will detail the Android app we built for testing this POC. As always, our goal is to use kinetic learning to drive conversation. In other words, none of this is production ready, it’s simply tools to allow us to drive design realtime.

MySQL [users]> desc beacon_sightings;
+-----------+--------------+------+-----+-------------------+-----------------------------+
| Field     | Type         | Null | Key | Default           | Extra                       |
+-----------+--------------+------+-----+-------------------+-----------------------------+
| client_id | varchar(100) | YES  |     | NULL              |                             |
| ts        | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| tx_power  | int(11)      | YES  |     | NULL              |                             |
| rssi      | int(11)      | YES  |     | NULL              |                             |
| beacon_id | varchar(100) | YES  |     | NULL              |                             |
+-----------+--------------+------+-----+-------------------+-----------------------------+
5 rows in set (0.00 sec)

MySQL [users]> select * from beacon_sightings limit 10;
+--------------------------------------+---------------------+----------+------+--------------------------------------+
| client_id                            | ts                  | tx_power | rssi | beacon_id                            |
+--------------------------------------+---------------------+----------+------+--------------------------------------+
| d8b69863-7186-4eb5-84dd-d2e9f48a8ed6 | 2017-03-12 11:23:58 |      -41 |  -55 | 0x0377656267617a6572080x000000000000 |
| d8b69863-7186-4eb5-84dd-d2e9f48a8ed6 | 2017-03-12 11:23:59 |      -41 |  -54 | 0x0377656267617a6572080x000000000000 |
| d8b69863-7186-4eb5-84dd-d2e9f48a8ed6 | 2017-03-12 11:24:00 |      -41 |  -73 | 0x0377656267617a6572080x000000000000 |
| d8b69863-7186-4eb5-84dd-d2e9f48a8ed6 | 2017-03-12 11:24:01 |      -41 |  -54 | 0x0377656267617a6572080x000000000000 |
| d8b69863-7186-4eb5-84dd-d2e9f48a8ed6 | 2017-03-12 11:24:03 |      -41 |  -74 | 0x0377656267617a6572080x000000000000 |
| d8b69863-7186-4eb5-84dd-d2e9f48a8ed6 | 2017-03-12 11:24:05 |      -41 |  -74 | 0x0377656267617a6572080x000000000000 |
| d8b69863-7186-4eb5-84dd-d2e9f48a8ed6 | 2017-03-12 11:24:06 |      -41 |  -70 | 0x0377656267617a6572080x000000000000 |
| d8b69863-7186-4eb5-84dd-d2e9f48a8ed6 | 2017-03-12 11:24:07 |      -41 |  -66 | 0x0377656267617a6572080x000000000000 |
| d8b69863-7186-4eb5-84dd-d2e9f48a8ed6 | 2017-03-12 11:24:08 |      -41 |  -66 | 0x0377656267617a6572080x000000000000 |
| d8b69863-7186-4eb5-84dd-d2e9f48a8ed6 | 2017-03-12 11:24:14 |      -41 |  -64 | 0x0377656267617a6572080x000000000000 |
+--------------------------------------+---------------------+----------+------+--------------------------------------+
10 rows in set (0.00 sec)

MySQL [users]> 

[root@poc01 dockerNode]# cat server.js
'use strict';
var bodyParser = require('body-parser')
var mysql = require('mysql');
const express = require('express');
const app = express();
app.use(bodyParser.urlencoded());

//this routine catches any post requests to the url noted and logs the post data into a mysql table

app.post("/beaconsighting",(req,res)=>{
  var connection = mysql.createConnection({host : '172.17.0.1',user : 'root', database: 'users', password : '123'});
  connection.query('insert into beacon_sightings(beacon_id,ts,tx_power,rssi,client_id) values(?,current_timestamp(),?,?,?)',
                   [req.body.beaconId,req.body.txPower,req.body.rssi,req.body.clientId], (err, results) => {
    if(err) {
      console.log(err);
      return err;
    }
  });
  connection.end();
  console.log(req.body);
  res.sendStatus(200);
});

app.listen(8080);
[root@poc01 dockerNode]# cat package.json
{
  "name": "docker_web_app",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "Steve Howard ",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "mysql": "latest",
    "node-mysql": "latest",
    "body-parser": "latest",
    "express": "latest"
  }
}
[root@poc01 dockerNode]# cat Dockerfile
FROM node:boron

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE 8080

CMD [ "npm", "start" ]
[root@poc01 dockerNode]#

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.