diff --git a/app.js b/app.js index 751916e7..3744a4e0 100644 --- a/app.js +++ b/app.js @@ -12,8 +12,8 @@ const LocalStrategy = require('passport-local').Strategy; const mongo = require('mongodb'); const mongoose = require('mongoose'); const async = require('async'); -const crypto = require('crypto'); // Required for generating a session secret -require('dotenv').config(); // Load environment variables from a .env file +const crypto = require('crypto'); +require('dotenv').config(); const routes = require('./routes/index'); const users = require('./routes/users'); @@ -26,7 +26,7 @@ const app = express(); // View engine setup app.set('views', path.join(__dirname, 'views')); -app.engine('handlebars', exphbs.engine({ defaultLayout: "main"})); +app.engine('handlebars', exphbs.engine({ defaultLayout: 'layout' })); app.set('view engine', 'handlebars'); // Middleware and configurations @@ -37,7 +37,7 @@ app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); // Express Session -const sessionSecret = crypto.randomBytes(64).toString('hex'); // Generate a session secret +const sessionSecret = crypto.randomBytes(64).toString('hex'); app.use(session({ secret: sessionSecret, saveUninitialized: true, @@ -71,7 +71,6 @@ app.use(flash()); // Makes the user object global in all views app.get('*', function (req, res, next) { - // Put user into res.locals for easy access from templates res.locals.user = req.user || null; if (req.user) { res.locals.type = req.user.type; @@ -127,4 +126,65 @@ app.use(function (err, req, res, next) { }); }); +// Get port from environment and store in Express +const port = normalizePort(process.env.PORT || '3000'); +app.set('port', port); + +// Create HTTP server +const http = require('http'); +const 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) { + const port = parseInt(val, 10); + + if (isNaN(port)) { + return val; + } + + if (port >= 0) { + return port; + } + + return false; +} + +// Event listener for HTTP server "error" event +function onError(error) { + if (error.syscall !== 'listen') { + throw error; + } + + const bind = typeof port === 'string' + ? 'Pipe ' + port + : 'Port ' + port; + + 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() { + const addr = server.address(); + const bind = typeof addr === 'string' + ? 'pipe ' + addr + : 'port ' + addr.port; + console.log('Listening on ' + bind); +} + module.exports = app; diff --git a/bin/www b/bin/www index 0ca6716a..d0a42f15 100644 --- a/bin/www +++ b/bin/www @@ -1,5 +1,3 @@ -#!/usr/bin/env node - /** * Module dependencies. */ diff --git a/package.json b/package.json index 427613f6..5ec1ab88 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.0.0", "private": true, "scripts": { - "start": "node ./bin/www", + "start": "node app.js", "lint": "standard" }, "dependencies": { diff --git a/reports/Ben_activity_log.txt b/reports/Ben_activity_log.txt index d29071de..a6215727 100644 --- a/reports/Ben_activity_log.txt +++ b/reports/Ben_activity_log.txt @@ -8,3 +8,5 @@ 2023-12-06T03:53:06.908Z - User: Ben, Activity: Logged In 2023-12-06T03:53:08.568Z - User: Ben, Activity: Accessed Classes Page 2023-12-06T03:53:10.758Z - User: Ben, Activity: Logged Out +2023-12-06T07:41:53.533Z - User: Ben, Activity: Provided correct password +2023-12-06T07:41:53.538Z - User: Ben, Activity: Logged In