Session is a tool to remember trusted users who has been logging into the backend or the dashboard. And next time, when they want to get into the dashboard, they will be directly sent to it without getting through the login page. However, to use this session tool, we need to install two packages.
$ npm install express-session
$ npm install connect-mongo
After successfully being installed, we can import the two packages into the entry point to setup a link to the MongoDB database as below:
// index.js
// npm install express
// npm install express-session
// npm install connect-mongo
import express from 'express'
const app = express()
import mydb from './model/conMongoDB.js'
app.use('/',async function(req,res,next){
req.mydb = await mydb
next()
})
import dotenv from 'dotenv'
dotenv.config()
import session from 'express-session'
import MongoStore from 'connect-mongo'
app.use(session({
store: MongoStore.create({mongoUrl:process.env.DATABASE_URI}),
secret: process.env.SECRET_KEY,
resave: false,
saveUninitialized: false
}))
import path from 'path'
const __dirname = path.resolve()
app.set('views', path.join(__dirname, 'view'))
app.set('view engine', 'ejs')
app.use(express.static(path.join(__dirname, 'static')))
import indexRoute from './route/index.js'
app.use('/',indexRoute)
import loginRoute from './route/login.js'
app.use('/',loginRoute)
const port = process.env.PORT || 8000
app.listen(port, () => {
console.log(`This app is listening to the port ${port}`)
})