How make a database connection on replit - Advanced Node and Express - Implement the Serialization of a Passport User

Hello
I’ve done several experiments to get the database connection to work, but it still doesn’t work…
here are my experiences:

On mongodb
I created a new account,
then I did a project called “test”
and when I open and than I click connect I get this code

mongodb+srv://wilfried:<password>@cluster0.gwu7zwh.mongodb.net/?retryWrites=true&w=majority

after taking this piece of code and replacing to paste it on
example.env

PORT=8080
NODE_ENV=development
MONGO_URI=''
MONGO_URI=mongodb+srv://wilfried:123456789@cluster0.gwu7zwh.mongodb.net/?retryWrites=true&w=majority

after trying to do on server.js
a variable :

let uri="mongodb+srv://wilfried:123456789@cluster0.gwu7zwh.mongodb.net/?retryWrites=true&w=majority"

and finally I click on the secret tab to add a MONGO_URI key with a value

mongodb+srv://wilfried:123456789@cluster0.gwu7zwh.mongodb.net/?retryWrites=true&w=majority

each time on replit.com the console output is:

Error: secret option required for sessions

Can someone help me please?

The secret option is not related to the connection string. It is its own environment variable.


As an aside. An example.env is not used for environment variables, it is just used as an example. Usually, you copy and rename it and add your own variables to it. It is called an example because it contains examples of the needed variables for the project which need to go into the actual .env file.

Ok
So all the code for the database connection is in server.js ?
I have to copy all my .env file and paste it in a variable in my server.js file ?
This is my console ouput :

express-session deprecated req.secret; provide secret option server.js:15:9
Listening on port 3000
Error: secret option required for sessions

and my code @ line 15 is :

app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
  cookie: { secure: false }
}));

but i still not understand the process to connect my database.

Where i have to put this piece of code ? in a variable in server.js ?

mongodb+srv://wilfried:123456789@cluster0.gwu7zwh.mongodb.net/?retryWrites=true&w=majority

The error is not for the DB connection it is for express-session.

app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
  cookie: { secure: false }
}));

process.env.SESSION_SECRET should be given a string value.

Open the secret tab on Replit and add the key SESSION_SECRET and give it some random string value. You can probably also just inline the string in the code.

app.use(session({
  secret: 'something random',
  resave: true,
  saveUninitialized: true,
  cookie: { secure: false }
}));

Just to be clear, the .env file is not used on Replit.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.