Tuesday 16 May 2017

Pass variable to express router

I want to pass my passport variable to my router class but can't seem to find out how to do this.

Server.js:

const session = require('express-session');
const path = require('path');
const passport = require('passport');
const routes = require('./routes')(passport); //pass variable here
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const mongoose = require('mongoose');
const configDB = require('./config/database.js');
const flash = require('connect-flash');
const app = express();

mongoose.connect(configDB.url);

app.use(express.static(path.join(__dirname, '/dist')));
app.use(cookieParser());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(session({
  name: 'sessionid',
  secret: 'changethis',
  resave: false,
  saveUninitialized: true,
  cookie: {secure: true}
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(routes);

app.listen(8080, function () {
  console.log('App started on port 8080');
});

/routes/index.ejs: (I want the passport variable here).

const routes = require('express').Router();
const path = require('path');
const api = require('./api');

routes.use('/api', api);

routes.get('/*', function (req, res) {
  res.sendFile(path.join(path.dirname(require.main.filename) + '/dist/index.html'));
});

module.exports = routes;

What do I need to do in the index.ejs to get the passport variable?



via Luud van Keulen

No comments:

Post a Comment