title: Node Express.js Middleware, end Response
body: <pre><code>validateRegister: async (req, res, next) =&gt; {
  
          UserModel.findOne({email:req.body.email}, (err, example) =&gt; {
              console.log(example);
              if(err) console.log(err);
              if(example) {
                  res.status(400).json({message: &quot;Email already registered!&quot;});
                  res.end() //next('route')
              }
          });
          console.log(&quot;test&quot;);
          const user = new UserModel(req.body);
          await user.save((err) =&gt; {
              if (err) return res.status(500).json({ message: &quot;Database issue!&quot; });
          }); 
  
          next();
      },
  </code></pre>
  <p>Ok, I tried to insert user data if it is  not already in the database using mongoose. If the User regarding the email is already in the database the response should  be ended and the user not inserted. I tried to end the response with res.end() and next('route'), but nothing seems to work, the console.log(&quot;test&quot;) still runs.</p>
  <p>Error:</p>
  <pre><code>events.js:353
        throw er; // Unhandled 'error' event
        ^
  
  Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
      at ServerResponse.setHeader (_http_outgoing.js:561:11)
  </code></pre>
  <p>Thanks for your help</p>