Currently, I am in the process of developing a web application using Next.js....
Within my app.js file located in the server directory, the following code is present:
const express = require("express");
const next = require("next");
const expressValidator = require("express-validator");
const mongoose = require("mongoose");
const passport = require("passport");
const mongoSessionStore = require("connect-mongo");
const session = require("express-session");
/* Loads all variables from .env file to "process.env" */
// npm install dotenv
// require("dotenv").config();
require("./models/Listing");
require("./models/User");
const routes = require("./routes");
const dev = process.env.NODE_ENV !== "production";
const port = process.env.PORT || 3000;
const ROOT_URL = dev ? `http://localhost:${port}` : process.env.PRODUCTION_URL;
const app = next({ dev });
const handle = app.getRequestHandler();
const mongooseOptions = {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
};
mongoose
.connect(
process.env.MONGODB_URI,
mongooseOptions
)
.then(() => console.log("DB connected"));
mongoose.connection.on("error", err => {
console.log(`DB connection error: ${err.message}`);
});
app.prepare().then(() => {
const server = express();
if(!dev) {
/* Helmet helps secure our app by setting various HTTP headers */
server.use(helmet());
/* Compression gives us gzip compression */
server.use(compression());
}
/* Body Parser built-in to Express as of version 4.16 */
server.use(express.json());
/* Express Validator will validate form data sent to the backend */
// server.use(expressValidator());
/* apply routes from the "routes" folder */
server.use("/", routes);
/* give all Next.js's requests to Next.js server */
server.get("/_next/*", (req, res) => {
handle(req, res);
});
server.get("/static/*", (req, res) => {
handle(req, res);
});
const MongoStore = new mongoSessionStore(session);
const sessionConfig = {
name: "next-connect.sid",
// secret used for using signed cookies w/ the session
secret: process.env.SESSION_SECRET,
store: new MongoStore({
mongooseConnection: mongoose.connection,
ttl: 14 * 24 * 60 * 60 // save session for 14 days
}),
// forces the session to be saved back to the store
resave: false,
// don't save unmodified sessions
saveUninitialized: false,
cookie: {
httpOnly: true,
maxAge: 1000 * 60 * 60 * 24 * 14 // expires in 14 days
}
};
if (!dev) {
sessionConfig.cookie.secure = true; // serve secure cookies in production environment
server.set("trust proxy", 1); // trust first proxy
}
/* Apply our session configuration to express-session */
server.use(session(sessionConfig));
/* Add passport middleware to set passport up */
server.use(passport.initialize());
server.use(passport.session());
server.use((req, res, next) => {
/* custom middleware to put our user data (from passport) on the req.user so we can access it as such anywhere in our app */
res.locals.user = req.user || null;
next();
});
server.get("*", (req, res) => {
handle(req, res);
});
server.listen(port, err => {
if (err) throw err;
console.log(`Server listening on ${ROOT_URL}`);
});
})
Upon running the code above, I am encountering the following error:
UnhandledPromiseRejectionWarning: TypeError: Class constructor MongoStore cannot be invoked without 'new'
It appears that the line causing this error is:
const MongoStore = new mongoSessionStore(session);
How can this issue be resolved?