title: Returning HTML or JSON based on header type Nodejs
body: <p>My nodeJS Api needs to return HTML or Json based on the header. If the header is:</p>
  
  <pre><code>Accept = application/json
  </code></pre>
  
  <p>The Api needs to return Json else my Api needs to return a HTML file.</p>
  
  <p>this is the code I use on my routes:</p>
  
  <pre><code>var app = express();
  app.use('/auth', require('./routes/Authenticate'));
  </code></pre>
  
  <p>In the Authenticate file I catch /login, and do the login stuff. if it succeeds I redirect to /users. In the /users I check for the Accept with an if statement:</p>
  
  <pre><code>router.get('/users', function(req,res){
      if(req.get('Accept') === 'application/json'){
         res.json({ success: true, user: req.user });
      } else {
          res.render("account/profile") //redirect to the file
      }
  });
  </code></pre>
  
  <p>This works(from this <a href="https://stackoverflow.com/questions/25497313/make-controller-return-html-or-json-depending-on-request-type">solution</a>) but is there a better way? Because there are like 20 endpoints and the application is growing and this will be a mess for every endpoint.</p>