Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added application logs with morgan #51

Merged
merged 2 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.env
.env
logs
56 changes: 35 additions & 21 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
require('dotenv').config();

const express = require('express');
const expressLayout = require('express-ejs-layouts');
const methodOverride = require('method-override');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const MongoStore = require('connect-mongo');
require("dotenv").config();

const express = require("express");
const expressLayout = require("express-ejs-layouts");
const methodOverride = require("method-override");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const MongoStore = require("connect-mongo");
const rfs = require("rotating-file-stream");
const passport = require('passport'); // Added passport for authentication
const flash = require('connect-flash'); // Added flash for storing flash messages

const connectDB = require('./server/config/db');
const { isActiveRoute } = require('./server/helpers/routeHelpers');
const connectDB = require("./server/config/db");
const { isActiveRoute } = require("./server/helpers/routeHelpers");
const morgan = require("morgan");

// Passport configuration
require('./server/config/passport')(passport); // Passport config to be created separately

const app = express();
const PORT = process.env.PORT || 5000;

const accessLogStream = rfs.createStream("application.log", {
interval: "1d",
path: "./logs",
});

app.use(morgan("combined", { stream: accessLogStream }));

// Connect to DB

app.use(session({
secret: 'your_secret_key', // Change this to your secret key
resave: false,
Expand All @@ -34,18 +45,21 @@ connectDB();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser());
app.use(methodOverride('_method'));
app.use(morgan("dev"));
app.use(methodOverride("_method"));

// Session setup for storing user session data
app.use(session({
secret: process.env.SESSION_SECRET || 'keyboard cat', // Use SESSION_SECRET from .env
resave: false,
saveUninitialized: true,
store: MongoStore.create({
mongoUrl: process.env.MONGODB_URI // Use MongoDB Atlas URI from .env
}),
// cookie: { maxAge: new Date(Date.now() + 3600000) } // Uncomment if you want custom cookie expiry time
}));
app.use(
session({
secret: process.env.SESSION_SECRET || "keyboard cat", // Use SESSION_SECRET from .env
resave: false,
saveUninitialized: true,
store: MongoStore.create({
mongoUrl: process.env.MONGODB_URI, // Use MongoDB Atlas URI from .env
}),
// cookie: { maxAge: new Date(Date.now() + 3600000) }// Uncomment if you want custom cookie expiry time
})
);

// Passport middleware
app.use(passport.initialize());
Expand All @@ -61,7 +75,7 @@ app.use((req, res, next) => {
});

// Static files
app.use(express.static('public'));
app.use(express.static("public"));

// Templating Engine
app.use(expressLayout);
Expand Down
130 changes: 130 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"nodemailer": "^6.9.15",
"passport": "^0.7.0",
"passport-local": "^1.0.0",
"morgan": "^1.10.0",
"rotating-file-stream": "^3.2.5",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down