Skip to content

Commit

Permalink
updated server.js file and added comments throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
fredm23579 committed Apr 13, 2024
1 parent 3a3b208 commit 35cda69
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const path = require('path'); // for file paths
const express = require('express'); // for server
const session = require('express-session'); // for sessions
const exphbs = require('express-handlebars'); // for handlebars
const methodOverride = require('method-override'); // for handling PUT requests
// Initializes Sequelize with session store
const SequelizeStore = require('connect-session-sequelize')(session.Store); // for storing sessions in db store (sequelize) instead of memory store (cookie)
const path = require('path'); // for file paths (https://nodejs.org/api/path.html)
const express = require('express'); // for server (https://expressjs.com/en/4x/api.html#app) or client (https://expressjs.com/en/4x/api.html#app)
const session = require('express-session'); // for sessions (https://expressjs.com/en/resources/middleware/session.html)
const exphbs = require('express-handlebars'); // for handlebars (https://expressjs.com/en/guide/using-template-engines.html)
const methodOverride = require('method-override'); // for handling PUT requests (method-override) (https://expressjs.com/en/resources/middleware/method-override.html)
// Initializes Sequelize with session store (https://expressjs.com/en/guide/behind-proxies.html)
const SequelizeStore = require('connect-session-sequelize')(session.Store); // for storing sessions in db store (sequelize) instead of memory store (cookie) (https://expressjs.com/en/guide/behind-proxies.html)

const app = express(); // initializes express
const PORT = process.env.PORT || 3001; // sets port to 3001 if not set in environment
const app = express(); // initializes express (https://expressjs.com/en/4x/api.html#app) or express server (https://expressjs.com/en/4x/api.html#app) or express client (https://expressjs.com/en/4x/api.html#app)
const PORT = process.env.PORT || 3001; // sets port to 3001 if not set in environment variables (default is 3001)

const routes = require('./controllers'); // routes for server and client side files (default is ./controllers/index.js)
const sequelize = require('./config/connection'); // for connecting to db with sequelize (default is ./config/connection.js)

app.use(methodOverride('_method')); // for handling PUT requests (method-override)
app.use(methodOverride('_method')); // for handling PUT requests (method-override) (https://expressjs.com/en/resources/middleware/method-override.html)

// Sets up session and connect to our Sequelize db
// Sets up session and connect to our Sequelize db store (sequelize) instead of cookie store (memory) (https://expressjs.com/en/guide/behind-proxies.html)
const sess = { // session settings for server and client side files (default is ./controllers/index.js)
secret: 'Super secret secret', // secret key for session cookie
secret: 'Super secret secret', // secret key for session cookie (default is 'Super secret secret')
cookie: { // session cookie settings for server and client side files (default is ./controllers/index.js)
maxAge: 30 * 60 * 1000, // 30 minutes (default is 10 minutes)
httpOnly: true, // default is true (https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#HTTP_only)
Expand All @@ -32,9 +32,9 @@ const sess = { // session settings for server and client side files (default is
}),
};

app.use(session(sess)); // session settings for server and client side files (default is ./controllers/index.js)
app.use(session(sess)); // session settings for server and client side files (default is ./controllers/index.js)

const hbs = exphbs.create(); // initializes handlebars
const hbs = exphbs.create(); // initializes handlebars (https://handlebarsjs.com/guide/) to handlebars (https://expressjs.com/en/guide/using-template-engines.html)

app.engine('handlebars', hbs.engine); // engine for handlebars (default is ./controllers/index.js)
app.set('view engine', 'handlebars'); // view engine for handlebars (default is ./controllers/index.js)
Expand All @@ -43,10 +43,10 @@ app.use(express.json()); // for parsing json data (https://expressjs.com/en/api.
app.use(express.urlencoded({ extended: false })); // for parsing url encoded data (https://expressjs.com/en/api.html#express.urlencoded)
app.use(express.static(path.join(__dirname, 'public'))); // for serving static files (https://expressjs.com/en/starter/static-files.html)

app.use(routes); // routes for server and client side files (default is ./controllers/index.js)
app.use(routes); // routes for server and client side files (default is ./controllers/index.js)

app.use((req, res, next) => { // middleware for checking if user is logged in or authenticated via session store (https://expressjs.com/en/guide/behind-proxies.html)
// List of paths that do not require authentication
// List of paths that do not require authentication (https://expressjs.com/en/guide/behind-proxies.html)
const authFreePaths = ['/login', '/signup', '/logout', '/css/', '/js/']; // paths that do not require authentication (https://expressjs.com/en/guide/behind-proxies.html)
if (!req.session.userId && !authFreePaths.some(path => req.path.startsWith(path))) { // if user is not logged in and path is not in authFreePaths list then redirect to login
return res.redirect('/login'); // redirect to login page (https://expressjs.com/en/guide/behind-proxies.html)
Expand All @@ -55,9 +55,9 @@ app.use((req, res, next) => { // middleware for checking if user is logged in or
});


// middleware for checking if session has expired
// middleware for checking if session has expired and if so then destroy session and redirect to login page (https://expressjs.com/en/guide/behind-proxies.html)
app.use((req, res, next) => { // middleware for checking if session has expired (https://expressjs.com/en/guide/behind-proxies.html)
if (req.session.expires && Date.now() > req.session.expires) { // if session has expired then destroy session and redirect to login
if (req.session.expires && Date.now() > req.session.expires) { // if session has expired then destroy session and redirect to login page (https://expressjs.com/en/guide/behind-proxies.html)
req.session.destroy(() => { // destroy session and redirect to login page (https://expressjs.com/en/guide/behind-proxies.html)
res.redirect('/login'); // redirect to login page (https://expressjs.com/en/guide/behind-proxies.html)
});
Expand All @@ -66,20 +66,20 @@ app.use((req, res, next) => { // middleware for checking if session has expired
}
});

// refreshes expiration time so site doesn't log out active users
// refreshes expiration time so site doesn't log out active users (https://expressjs.com/en/guide/behind-proxies.html)
app.use((req, res, next) => { // middleware for checking if session has expired (https://expressjs.com/en/guide/behind-proxies.html)
if (req.session.expires) {
const extendedExpirationTime = new Date(Date.now() + 30 * 60 * 1000); // 30 minutes (30 * 60 * 1000) = 30 minutes (default is 10 minutes)
req.session.expires = extendedExpirationTime; // refreshes expiration time so site doesn't log out active users
req.session.expires = extendedExpirationTime; // refreshes expiration time so site doesn't log out active users (https://expressjs.com/en/guide/behind-proxies.html)
}
next(); // next middleware (https://expressjs.com/en/guide/behind-proxies.html)
});

// middleware for checking if user is logged in or authenticated via session store (https://expressjs.com/en/guide/behind-proxies.html)
sequelize.sync({ force: false }).then(() => { // syncs sequelize with db (default is ./config/connection.js)
app.listen(PORT, () => // listens on port 3001 (default is 3001)
app.listen(PORT, () => // listens on port 3001 (default is 3001)
console.log(
`\nServer running on port ${PORT}. Visit http://localhost:${PORT} and create an account!`
`\nServer running on port ${PORT}. Visit http://localhost:${PORT} and create an account!` // logs server running on port 3001 (default is 3001) (https://expressjs.com/en/guide/behind-proxies.html
)
);
});

0 comments on commit 35cda69

Please sign in to comment.