add simple frontend

This commit is contained in:
Shannon Sterz 2016-01-12 17:49:13 +01:00
parent 5024953ac8
commit 066e2cca08
10 changed files with 266 additions and 19 deletions

60
nodejs/app.js Normal file
View file

@ -0,0 +1,60 @@
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;

90
nodejs/bin/www Executable file
View file

@ -0,0 +1,90 @@
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('nodejs:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}

View file

@ -1,23 +1,18 @@
{ {
"name": "auto-home", "name": "nodejs",
"version": "1.0.0", "version": "0.0.0",
"description": "A way of controlling your home, hopefully.", "private": true,
"main": "index.js",
"dependencies": {
"serialport": "^2.0.6"
},
"devDependencies": {},
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "start": "node ./bin/www"
}, },
"repository": { "dependencies": {
"type": "git", "body-parser": "~1.13.2",
"url": "https://svs.ankaa.uberspace.de/sterzy/auto-home.git" "cookie-parser": "~1.3.5",
}, "debug": "~2.2.0",
"keywords": [ "ejs": "~2.3.3",
"home", "express": "~4.13.1",
"automation" "morgan": "~1.6.1",
], "serialport": "^2.0.6",
"author": "Stefan Sterz", "serve-favicon": "~2.3.0"
"license": "ISC" }
} }

35
nodejs/public/javascripts/angularApp.js vendored Normal file
View file

@ -0,0 +1,35 @@
var app = angular.module('autome', ['ui.router']);
app.config(['$stateProvider', '$urlRouterProvider',function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: '/home.html',
controller: 'MainCtrl',
});
$urlRouterProvider.otherwise('home');
}]);
app.factory('arduino', ['$http', function($http, auth){
var o = {};
o.toggleLight = function ( ) {
return $http.get('/toggleLight');
}
return o;
}]);
app.controller('MainCtrl', ['$scope', 'arduino', function ($scope, arduino){
$scope.test = 'Hello World!';
$scope.toggleLight = function(){
};
}]);

View file

@ -0,0 +1,6 @@
<div class="page-header">
<h1>Autome</h1>
<form ng-submit="toggleLight()" style="margin-top:30px;">
<button type="submit" class="btn btn-primary">Post</button>
</form>
</div>

View file

@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: #00B7FF;
}

9
nodejs/routes/index.js Normal file
View file

@ -0,0 +1,9 @@
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
module.exports = router;

9
nodejs/routes/users.js Normal file
View file

@ -0,0 +1,9 @@
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
module.exports = router;

3
nodejs/views/error.ejs Normal file
View file

@ -0,0 +1,3 @@
<h1><%= message %></h1>
<h2><%= error.status %></h2>
<pre><%= error.stack %></pre>

32
nodejs/views/index.ejs Normal file
View file

@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<title>Auto Home</title>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>
<script src="/javascripts/angularApp.js"></script>
<style> .glyphicon-thumbs-up { cursor:pointer } </style>
</head>
<body ng-app="autome">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<ui-view></ui-view>
</div>
</div>
<script src="/snippets/home.html" type="text/ng-template" id="/home.html">
<div class="page-header">
<h1>Autome</h1>
<form ng-submit="toggleLight()" style="margin-top:30px;">
<button type="submit" class="btn btn-primary">Light Switch</button>
</form>
</div>
</script>
</body>
</html>