Created
February 18, 2022 09:55
-
-
Save ashwath007/a4b729f6728572cb81fad86191f33f64 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require("dotenv").config(); | |
const cors = require('cors'); | |
const express = require('express'); | |
const mongoose = require('mongoose'); | |
const session = require('express-session') | |
const bodyParser = require("body-parser"); | |
const cookieParser = require("cookie-parser"); | |
const Pig = require('pigcolor'); | |
const app = express(); | |
// app.set('trust proxy', 1) // trust first proxy | |
// app.use(session({ | |
// secret: process.env.SECRET_SESSION, | |
// resave: false, | |
// saveUninitialized: true, | |
// secure: true, | |
// cookie: { sameSite: 'none', httpOnly: true, secure: true, maxAge: 1000 * 60 * 60 * 24 } // 1 Day Time Period | |
// })) | |
// app.use( | |
// session({ | |
// secret: 'secret', | |
// resave: true, | |
// saveUninitialized: false, | |
// cookie: { sameSite: 'none', secure: true, maxAge: 60 * 60 * 1000 }, | |
// }) | |
// ); | |
if (app.get('env') == 'production') { | |
app.enable('trust proxy') | |
sessionOptions.cookie.secure = true | |
} | |
//Middlewares | |
app.use(bodyParser.json()); | |
app.use(cookieParser()); | |
const corsConfig = { | |
origin: 'https://localhost:8080', | |
credentials: true, | |
}; | |
app.use(cors(corsConfig)); | |
// *? ------------------------------------------------ | |
// ** ------------ 1. All Router Checkpoints -------------------- | |
const routerCheckPointRoute = require('./auth/auth_routes'); | |
// ** -------------------------------------------------------- | |
// *? ------------------------------------------------ | |
//DB Connection | |
mongoose | |
.connect(process.env.DATABASE_D, { | |
useNewUrlParser: true, | |
useUnifiedTopology: true, | |
}) | |
.then(() => { | |
Pig.db(); | |
}); | |
// Session Store Connection | |
//Mongo DB Store configuraton for session storage | |
const MongoDBStore = require('connect-mongodb-session')(session); | |
const store = new MongoDBStore({ | |
uri: `${process.env.DATABASE_D}`, | |
collection: 'session' | |
}); | |
store.on('error', function(error) { | |
console.log(error); | |
}); | |
let sessionOptions = { | |
key: 'SID', | |
secret: process.env.SECRET_SESSION, | |
cookie: { | |
expires: 1000 * 60 * 60 * 5, //5hr | |
// sameSite:'None', | |
httpOnly: true, | |
sameSite: 'none', | |
secure: true, | |
// secure: true, | |
// domain: 'localhost:3000' | |
}, | |
store: store, | |
resave: false, | |
saveUninitialized: false, | |
unset: 'destroy', | |
rolling: true | |
} | |
app.use(session(sessionOptions)) | |
// Port | |
const Port = process.env.PORT || 8000; | |
// ** Testing | |
app.get("/", (req, res) => { | |
if (req.session.viewCount) { | |
req.session.viewCount++; | |
} else { | |
req.session.viewCount = 1; | |
} | |
// res.cookie("connect.sid", req.sessionID, { sameSite: 'none', secure: true, maxAge: 1000 * 60 * 60 * 24 }); | |
res.json({ | |
msg: "Willow MVP 1.0.0", | |
sesssionID: req.sessionID, | |
count: req.session.viewCount | |
}) | |
}); | |
// ?? --------------------------------------------- | |
// ** ------------ 1. All Router Checkpoints -------------------- | |
app.use('/api/web', routerCheckPointRoute); | |
// ** -------------------------------------------------------- | |
// ?? --------------------------------------------- | |
// Starting a port here | |
app.listen(Port, () => { | |
Pig.server(Port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment