├── Procfile ├── .gitignore ├── app.json ├── package.json ├── README.md ├── public ├── js │ └── example.js └── index.html └── app.js /Procfile: -------------------------------------------------------------------------------- 1 | web: node app.js 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .git 3 | -------------------------------------------------------------------------------- /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stsplatform-skeleton-express", 3 | "description": "An Express skeleton app using the STS Platform", 4 | "repository": "https://github.com/SenseTecnic/stsplatform-skeleton-app-express", 5 | "keywords": ["nodejs", "express", "stsplatform"] 6 | } 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stsplatform-skeleton-express", 3 | "version": "0.1.0", 4 | "description": "An Express skeleton app using the STS Platform", 5 | "main": "app.js", 6 | "scripts": { 7 | "start": "node app.js" 8 | }, 9 | "dependencies": { 10 | "body-parser": "^1.13.3", 11 | "cookie-parser": "^1.3.5", 12 | "express": "^4.13.1", 13 | "stsplatform": "^0.1.0" 14 | }, 15 | "engines": { 16 | "node": "0.12.7" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/SenseTecnic/stsplatform-skeleton-app-express" 21 | }, 22 | "keywords": [ 23 | "node", 24 | "stsplatform", 25 | "express" 26 | ], 27 | "license": "MIT" 28 | } 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A Skeleton Express and STS Platform App 2 | ======================================= 3 | 4 | This is a skeleton application written in NodeJS and using the [Express 4](http://expressjs.com/) framework. 5 | 6 | ## Quick Deploy on Heroku: 7 | 8 | [](https://heroku.com/deploy) 9 | 10 | ## What this application has: 11 | 12 | This bare-bones application will bootstrap your application development by providing a pre-configured simple application with: 13 | 14 | * A bare-bones Express application 15 | * Bootstrap styling 16 | * The stsplatform module 17 | 18 | For more information and support visit [http://developers.sensetecnic.com](http://developers.sensetecnic.com) 19 | -------------------------------------------------------------------------------- /public/js/example.js: -------------------------------------------------------------------------------- 1 | jQuery(function($) { 2 | $('form[data-async]').on('submit', function(event) { 3 | 4 | var $form = $(this); 5 | var $target = $($form.attr('data-target')); 6 | var button_method = $form.find('button[clicked=true]').attr('data-method'); 7 | $.ajax({ 8 | method: button_method !== undefined ? button_method : $form.attr('method'), 9 | url: $form.attr('action'), 10 | data: $form.serialize(), 11 | 12 | success: function(data, status) { 13 | $target.html(data); 14 | } 15 | }); 16 | event.preventDefault(); 17 | 18 | if ($form.attr('data-modal') === "true") { 19 | $('#credentialsModal').modal('hide'); //In the case of the modal, close it. 20 | } 21 | 22 | }); 23 | 24 | $("form button[type=submit]").click(function() { 25 | $("button[type=submit]", $(this).parents("form")).removeAttr("clicked"); 26 | $(this).attr("clicked", "true"); 27 | }); 28 | 29 | }); 30 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var sts = require('stsplatform'); 3 | var cookieParser = require('cookie-parser'); 4 | var bodyParser = require('body-parser'); 5 | var urlencodedParser = bodyParser.urlencoded({ extended: false }); 6 | 7 | var app = express(); 8 | app.use('/', express.static(__dirname + '/public')); 9 | app.use(cookieParser('averysecretestring-sdfwe1#$%F@!#F')); 10 | 11 | app.get('/data', function (req, res) { 12 | if (!req.cookies.key_id) { 13 | res.send('You must set your credentials'); 14 | } else { 15 | var response = sts_data_resource(req).get({beforeE:1}).then(function(response){ 16 | res.send(JSON.stringify(response.data)); 17 | }); 18 | } 19 | }); 20 | 21 | app.post('/data', urlencodedParser, function(req, res){ 22 | if (!req.cookies.key_id) { 23 | res.send('You must set your credentials'); 24 | } else { 25 | var response = sts_data_resource(req).post({value:req.body.value}).then(function(response){ 26 | res.send("Value "+req.body.value+" sent to wotkit, received code "+response.code); 27 | }); 28 | } 29 | }); 30 | 31 | app.post('/auth', urlencodedParser, function(req, res){ 32 | var minute=60*1000, hour=minute*60, day=hour*24; 33 | res.cookie('key_id', req.body.key_id, { maxAge: day }); 34 | res.cookie('key_password', req.body.key_password, { maxAge: day }); 35 | res.cookie('sensor_name', req.body.sensor_name, { maxAge: day }); 36 | res.send('Credentials Set'); 37 | }); 38 | 39 | function sts_data_resource(req){ 40 | var client = new sts.Client( 41 | {auth:{ 42 | key_id:req.cookies.key_id, 43 | key_password:req.cookies.key_password 44 | } 45 | } 46 | ); 47 | var sensor = new sts.Sensors(client,req.cookies.sensor_name); 48 | return new sts.Data(sensor); 49 | } 50 | 51 | var server = app.listen(process.env.PORT || 3000, function () { 52 | var host = server.address().address; 53 | var port = server.address().port; 54 | console.log('Example app listening at http://%s:%s', host, port); 55 | }); 56 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 |