Prasana Ramesh Prasana Ramesh - 4 months ago 20
Node.js Question

How to export session variables between files in Nodejs?

I currently have in my server.js file:

const neo4j_conn = 'bolt://' +;
const driver = neo4j.driver(neo4j_conn, neo4j.auth.basic(config.neo4j.username, config.neo4j.passphrase));
const session = driver.session();
app.set('neo4jsession', session);


and in my routes file I have :

const session = app.get('neo4jsession');

considering that I need only one session per request, is this the right approach for me to connect to the database and get a session back? Can you suggest a better design pattern/ coding method/ for this purpose?


In your example, you open the session, and it is one for the entire server.

You need pass function instead result of function:

app.set('neo4jsession', driver.session);

And if we take the example of the router out of the box:

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  const session ='session')();      
    .run( query )
    .then( function(result)
      res.json( result );

module.exports = router;