Skip to content

Commit

Permalink
new updates
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanMendez01 committed Dec 6, 2023
1 parent d58af44 commit af5ed65
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
70 changes: 65 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
2 changes: 0 additions & 2 deletions bin/www
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"start": "node app.js",
"lint": "standard"
},
"dependencies": {
Expand Down
2 changes: 2 additions & 0 deletions reports/Ben_activity_log.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit af5ed65

Please sign in to comment.