Skip to content

Commit

Permalink
Add application logs with morgan
Browse files Browse the repository at this point in the history
  • Loading branch information
susrithasabbini authored Oct 12, 2024
1 parent 61155ef commit 3bf1fa9
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 22 deletions.
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

0 comments on commit 3bf1fa9

Please sign in to comment.