Q:

After refresh "Cannot set headers after they are sent to the client"

I am using express js, mongoose and ejs as templates. 
When I refresh my page, in second or third try, I get this error. 
Here is my app.js :
{consts..}

var index = require('./routes/index.js');
var users = require('./routes/users.js');

app.set("port", ("2401"));
app.set("views", __dirname + "/views");
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, 'public')));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use('/', index);
app.use('/users', users);

var port = '2401';
app.set('port', port);

var server = http.createServer(app);

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  console.log('server started on port : ' + app.get('port'));
}

users router :
{consts..}

router.get('/login', function (req, res) {
   users.getLogin(req, res);
});

router.post('/login', [
    check('email')
        .isEmail().withMessage('Please enter a valid email address')
        .trim()
        .normalizeEmail(),
    check('terms', 'Please accept our terms and conditions').equals('yes'),
],
    function (req, res) {
        users.postLogin(req, res);
    });

module.exports = router;

and controller : 
usersController.getLogin = function (req, res) {
    const connector = mongoose.connect(conStr, { useNewUrlParser: true, useUnifiedTopology: true });
    mongoose.connection.on('open', function (ref) {
        categories.find(function (err, cats) {
            if (err) {
                console.log("Error:", err);
            }
            else {
                    return res.render("../views/login.ejs", {
                    _: _,
                    categories: cats
                });
            }
        });
    });
}

index router :
var express = require('express');
var router = express.Router();
var index = require("../controllers/indexController.js");

router.get('/', function (req, res) {
  index.list(req, res);
});
module.exports = router;

index controller: 
var mongoose = require("mongoose");
var categories = require("../models/categories.js");
var _ = require("underscore");

var indexController = {};

indexController.list = function (req, res) {
  const connector = mongoose.connect(conStr, { useNewUrlParser: true, useUnifiedTopology: true });
  mongoose.connection.on('open', function (ref) {
    categories.find(function (err, cats) {
      if (err) {
        console.log("Error:", err);
      }
      else {
        console.log("indexe geldi");
        return res.render("../views/index.ejs", {
          _: _,
          categories: cats,
          sess: req.session
        });
      }
    })
  })
};

module.exports = indexController;

I tried almost everything but still no progress. Can it be about usage of "next()" ? 
I also tried the hole router get / post functions with next, mongoose find functions with exec().

A:

The issue is that you're opening a connection to mongo in every route and:
mongoose.connection.on('open' is firing for past requests, requests that are already finished, that's why you're getting Cannot set headers after they are sent to the client.
Move the mongoose.connect & the listener outside of each route.
usersController.getLogin = function (req, res) {

    categories.find(function (err, cats) {
            if (err) {
                console.log("Error:", err);
                return res.status(500).send('error');
            }
            else {
                    return res.render("../views/login.ejs", {
                    _: _,
                    categories: cats
                });
            }
      });

}

