diff --git a/nodejs/app.js b/nodejs/app.js new file mode 100644 index 0000000..03f8595 --- /dev/null +++ b/nodejs/app.js @@ -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; diff --git a/nodejs/bin/www b/nodejs/bin/www new file mode 100755 index 0000000..fbb654c --- /dev/null +++ b/nodejs/bin/www @@ -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); +} diff --git a/nodejs/package.json b/nodejs/package.json index 5675889..bdeeeeb 100644 --- a/nodejs/package.json +++ b/nodejs/package.json @@ -1,23 +1,18 @@ { - "name": "auto-home", - "version": "1.0.0", - "description": "A way of controlling your home, hopefully.", - "main": "index.js", - "dependencies": { - "serialport": "^2.0.6" - }, - "devDependencies": {}, + "name": "nodejs", + "version": "0.0.0", + "private": true, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "start": "node ./bin/www" }, - "repository": { - "type": "git", - "url": "https://svs.ankaa.uberspace.de/sterzy/auto-home.git" - }, - "keywords": [ - "home", - "automation" - ], - "author": "Stefan Sterz", - "license": "ISC" + "dependencies": { + "body-parser": "~1.13.2", + "cookie-parser": "~1.3.5", + "debug": "~2.2.0", + "ejs": "~2.3.3", + "express": "~4.13.1", + "morgan": "~1.6.1", + "serialport": "^2.0.6", + "serve-favicon": "~2.3.0" + } } diff --git a/nodejs/public/javascripts/angularApp.js b/nodejs/public/javascripts/angularApp.js new file mode 100644 index 0000000..eb905ff --- /dev/null +++ b/nodejs/public/javascripts/angularApp.js @@ -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(){ + + + }; + +}]); \ No newline at end of file diff --git a/nodejs/public/snippets/home.html b/nodejs/public/snippets/home.html new file mode 100644 index 0000000..8ffaf73 --- /dev/null +++ b/nodejs/public/snippets/home.html @@ -0,0 +1,6 @@ +
<%= error.stack %>diff --git a/nodejs/views/index.ejs b/nodejs/views/index.ejs new file mode 100644 index 0000000..d58789c --- /dev/null +++ b/nodejs/views/index.ejs @@ -0,0 +1,32 @@ + + + +