Prof. J. Walter - Informationstechnik, Mikrocomputertechnik, Digitale Medien Softwaredoku
Hochschule Karlsruhe Logo Informationstechnik Mechatronik MEB
Rainbow Log Server
Sommersemester 2020
Rohlf Devon, RODE1011
Bartman Thomas, BATH1021

Software Dokumentation

Die Webanwendung RainbowLOG mit NOTE.JS
App.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

//======================================================================================
//connection to MongoDB
var monk = require('monk');
//mount the nodetest1 database -> contains the "collections"/ data tables resides in /var/lib/mongodb on Linux
var db = monk('localhost:27017/nodetest1');

//=======================================================================================
//define paths to routing tables
var indexRouter = require('./routes/index');
var patientRouter = require('./routes/patientroutes');
var personalRouter = require('./routes/personalroutes');
//======================================================================================
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//======================================================================================
//Open the interface for the DB to the routing program
app.use(function(req,res,next){
  req.db = db;
  next();
});
//=====================================================================================
//define routing switches
app.use('/', indexRouter);
app.use('/patient', patientRouter);
app.use('/personal', personalRouter);
//======================================================================================
// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});
// //======================================================================================
//This export function should remain the LAST LINE
module.exports = app;

Index.js
var express = require('express');
var router = express.Router();
//===================================================================================================
//Post functionality for adding Help Request records to database from newpatient.ejs form in body!
// router.post('/addpatient', function(req, res){
//   var db = req.db;
//
//   //capture request variables for handoff to database
//   var BedNumber = req.body.BedNumber;
//   var Help = req.body.Help;
//   var Prio = req.body.Prio;
//
//   var collection = db.get('usercollect');
//
//   //insert new information to usercollect database
//   collection.insert({
//     "BedNumber": BedNumber,
//     "Help": Help,
//     "Prio": Prio,
//     "TimeIn": Date()
//   }, function (err, doc){
//     if(err) {
//       //error handling
//       res.send("Error in adding record to database");
//     }
//     else{
//       //forward to patient page
//       res.redirect("patient/patient");
//     }
//   });
// });
//===================================================================================================
// GET functions - Routes requests to the proper html/ejs pages!
//direct to homepage/index - two options!
router.get('/', function(req, res) {
  res.render('index', { title: 'Express' });
});
router.get('/index', function(req, res) {
  res.render('index', { title: 'Express' });
});
router.get('/personalindex', function(req, res) {
  res.render('personalindex', { title: 'Express' });
});
router.get('/patientindex', function(req, res) {
  res.render('patientindex', { title: 'Express' });
});
//====================================================================================================
//This export function should remain the LAST LINE in the file
module.exports = router;

www.js
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.
 */
//======================================================================================
var io = require('socket.io').listen(server.listen(3000));
//var io = socketIO(server);
//connection to MongoDB
var monk = require('monk');
//mount the nodetest1 database -> contains the "collections"/ data tables resides in /var/lib/mongodb on Linux
var db = monk('localhost:27017/nodetest1');
var collection = db.get('usercollect');
io.on('connection', socket => {
  console.log("New client connected "+ socket.id);

  //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  //Add new patient requests to the database
  socket.on('newReq', nReq => {
    console.log("Recieved: " + nReq.oBedNumber + ' '+ nReq.oHelp)
    var Time = Date();
    collection
        .insert( {bedNumber: nReq.oBedNumber, Help: nReq.oHelp, extPrio: nReq.oExtPrio, intPrio: nReq.oIntPrio, Time: [ Time, "No Entry", "No Entry"] })
        .then(updatedDoc => {
          io.sockets.emit('change_data');
        });
  }); //terminator for newpat
  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  //


});     //terminator for io.on
//======================================================================================
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);
}

patientroutes.js
var express = require('express');
var router = express.Router();

/* GET users listing. */
// direct to patient page
router.get('/patient', function(req, res, next){
  res.render('patient', {title: 'Express'});
});
router.get('/patientindex', function(req, res, next){
  res.render('patientindex', {title: 'Express'});
});
//direct to page for adding new patient entries -------- to be modified/reused later
router.get('/newpatient', function(req, res){
  res.render('newpatient', {title: 'Express'});
});
router.get('/1', function(req, res){
  res.render('1', {title: 'Express'});
});
router.get('/2a', function(req, res){
  res.render('2a', {title: 'Express'});
});
router.get('/2b', function(req, res){
  res.render('2b', {title: 'Express'});
});
router.get('/2c', function(req, res){
  res.render('2c', {title: 'Express'});
});
router.get('/2d', function(req, res){
  res.render('2d', {title: 'Express'});
});
router.get('/2e', function(req, res){
  res.render('2e', {title: 'Express'});
});
router.get('/3', function(req, res){
  res.render('3', {title: 'Express'});
});
module.exports = router;

personalroutes.js
var express = require('express');
var router = express.Router();

//direct to nurse page
router.get('/personal', function(req, res){
  res.render('personal', {title: 'Express'});
});
router.get('/personalindex', function(req, res){
  res.render('personalindex', {title: 'Express'});
});
router.get('/personalprotokoll', function(req, res){
  res.render('personalprotokoll', {title: 'Express'});
});
//direct to page for basic output of database info ------ to be modified/reused later
router.get('/reqlist', function(req, res){
  var db = req.db;
  var collection = db.get('usercollect');
  collection.find({},{}, function (e, docs) {
    res.render('reqlist',{
      "reqlist" : docs
    });
  });
});
module.exports = router;

reqlist.ejs - Print all requests in database out to user
<!DOCTYPE html>
<html>
<head>
    <title>Request List</title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
    <script type = "text/javascript">
        <!--
        function AutoRefresh( t ){
            setTimeout("location.reload(true);", t)
        }
        //-->
    </script>
</head>
<body onload ="JavaScript:AutoRefresh(10000);">
<h1>Request List</h1>
    <u1>
        <%
        var list = '';
        for (i = 0; i < reqlist.length; i++){
            list += '<li>ID: ' + reqlist[i]._id + ' Bed Number: ' + reqlist[i].BedNumber + ' Request Type: ' + reqlist[i].Help + ' Prio: ' + reqlist[i].Prio + ' Time Logged In: ' + reqlist[i].Time[0] +'</li>';
        }
        %>
        <%- list %>
    </u1>
    <li><a href = "personalindex">Return to Index</a></li>
</body>
</html>

  Mit Unterstützung von Prof. J. Walter Sommersemester 2020