Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

helpers.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. var fs = require('fs');
  2. var protocols = {
  3. http : require('http'),
  4. https : require('https')
  5. }
  6. var keys = {
  7. cert : fs.readFileSync(__dirname + '/keys/ssl.cert'),
  8. key : fs.readFileSync(__dirname + '/keys/ssl.key')
  9. }
  10. var helpers = {};
  11. helpers.server = function(opts, cb) {
  12. var defaults = {
  13. code : 200,
  14. headers : {'Content-Type': 'application/json'}
  15. }
  16. var mirror_response = function(req) {
  17. return JSON.stringify({
  18. headers: req.headers,
  19. body: req.body
  20. })
  21. }
  22. var get = function(what) {
  23. if (!opts[what])
  24. return defaults[what];
  25. if (typeof opts[what] == 'function')
  26. return opts[what](); // set them at runtime
  27. else
  28. return opts[what];
  29. }
  30. var finish = function(req, res) {
  31. if (opts.handler) return opts.handler(req, res);
  32. res.writeHead(get('code'), get('headers'));
  33. res.end(opts.response || mirror_response(req));
  34. }
  35. var handler = function(req, res) {
  36. req.setEncoding('utf8'); // get as string
  37. req.body = '';
  38. req.on('data', function(str) { req.body += str })
  39. req.socket.on('error', function(e) {
  40. // res.writeHead(500, {'Content-Type': 'text/plain'});
  41. // res.end('Error: ' + e.message);
  42. })
  43. setTimeout(function(){
  44. finish(req, res);
  45. }, opts.wait || 0);
  46. };
  47. var protocol = opts.protocol || 'http';
  48. var server;
  49. if (protocol == 'https')
  50. server = protocols[protocol].createServer(keys, handler);
  51. else
  52. server = protocols[protocol].createServer(handler);
  53. server.listen(opts.port, cb);
  54. return server;
  55. }
  56. module.exports = helpers;