Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Created October 18, 2019 23:30
Show Gist options
  • Select an option

  • Save harrisonmalone/c3d0673f6b0c7644d0dc6914b073daac to your computer and use it in GitHub Desktop.

Select an option

Save harrisonmalone/c3d0673f6b0c7644d0dc6914b073daac to your computer and use it in GitHub Desktop.
example of an express session
const express = require("express");
const morgan = require("morgan");
const expressSession = require("express-session");
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo')(expressSession);
const app = express();
const dbOptions = { useNewUrlParser: true, useUnifiedTopology: true }
mongoose.connect("mongodb://localhost:27017/test-with-mongo-store", dbOptions, (err) => {
if (err) {
console.log('not connected ❌')
} else {
console.log('connected ✅')
}
})
app.use(expressSession({
secret: 'secret',
resave: false,
saveUninitialized: false
}));
app.use(morgan('dev'));
app.get("/increment", (req, res) => {
req.session.pageCount = req.session.pageCount ? req.session.pageCount + 1 : 1;
res.send({viewed: req.session.pageCount});
})
app.listen(5000, () => console.log('listening on port 5000'));
{
"name": "backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"connect-mongo": "^3.0.0",
"express": "^4.17.1",
"express-session": "^1.17.0",
"mongoose": "^5.7.5",
"morgan": "^1.9.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment