title: Which part of app.js file is my index.js file missing?
body: <p>This works
  app.js</p>
  <pre><code>const express = require(&quot;express&quot;);
  const app = express();
  const mongoose = require(&quot;mongoose&quot;);
  const session = require(&quot;express-session&quot;);
  const passport = require(&quot;passport&quot;);
  const crypto = require(&quot;crypto&quot;);
  const routes = require(&quot;./routes&quot;);
  const connection = require(&quot;./config/database&quot;);
  const cors = require(&quot;cors&quot;);
  app.use(cors({ origin: &quot;http://localhost:3001&quot;, credentials: true }));
  const User = mongoose.models.User;
  
  const bodyParser = require(&quot;body-parser&quot;);
  
  const MongoStore = require(&quot;connect-mongo&quot;)(session);
  
  require(&quot;dotenv&quot;).config();
  
  app.use(express.json());
  
  app.use(express.urlencoded({ extended: true }));
  
  const sessionStore = new MongoStore({
    mongooseConnection: mongoose.connection,
    collection: &quot;sessions&quot;,
  });
  
  app.use(
    session({
      secret: &quot;zdfbdaf&quot;,
      resave: true,
      saveUninitialized: true,
      store: sessionStore,
      cookie: {
        cookie: { secure: false },
        maxAge: 1000 * 60 * 60 * 24,
      },
    })
  );
  function getUser(req, res, next) {
    User.findOne({ url: req.body.url })
      .then((user) =&gt; {
        if (!user) {
          console.log(&quot;No user Found&quot;);
          res.send(&quot;No user found&quot;);
        } else {
          console.log(&quot;Sup&quot;);
  
          res.status(200).send(user);
        }
      })
      .catch((err) =&gt; {
        done(err);
      });
  }
  
  app.post(&quot;/visitor&quot;, getUser);
  
  require(&quot;./config/passport&quot;);
  
  app.use(passport.initialize());
  app.use(passport.session());
  
  app.use(routes);
  
  app.listen(3000);
  </code></pre>
  <p>This does not</p>
  <p>index.js</p>
  <pre><code>const router = require(&quot;express&quot;).Router();
  const passport = require(&quot;passport&quot;);
  const bodyParser = require(&quot;body-parser&quot;);
  const genPassword = require(&quot;../lib/passwordUtils&quot;).genPassword;
  const connection = require(&quot;../config/database&quot;);
  const mongoose = require(&quot;mongoose&quot;);
  const User = mongoose.models.User;
  const isAuth = require(&quot;./authMiddleware&quot;).isAuth;
  
  function getUser(req, res, next) {
    User.findOne({ url: req.body.url })
      .then((user) =&gt; {
        if (!user) {
          console.log(&quot;No user Found&quot;);
          res.send(&quot;No user found&quot;);
        } else {
          console.log(&quot;Sup&quot;);
  
          res.status(200).send(user);
        }
      })
      .catch((err) =&gt; {
        console.log(err);
      });
  }
  
  router.post(&quot;/visitor&quot;, getUser);
  
  
  module.exports = router;
  </code></pre>
  <p>The Error is &quot;Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client&quot; and I cant seem to find where I'm using &quot;res&quot; before or during getUser thats screwing it up. I'm p[retty sure my error is that app.js has something that my index.js doesnt, im just not sure what. I tried moving things around, like cors, but that didnt help.</p>
  <p>(I comment out the app.visitor post route and getUser function when using the index page fo it, of course)</p>