title: How to set Content-Type header in node.js
body: <p>I am executing the following code in node.js. The code runs fine, but the tutorial tells us that :</p>
  
  <blockquote>
    <p><strong>Now go back and add the Content-Type header with a value of application/json and run the request again. You will get the “You sent
    JSON” message back from the server.</strong></p>
  </blockquote>
  
  <p>1) I am not able to understand how can I set headers for this program!</p>
  
  <p>2) Also If I am running the program without setting headers, then the message 'Server requires application/json' should be displayed. I am not seeing it being displayed anywhere. Where should it be displayed?</p>
  
  <pre><code>const express = require('express');
  const app = express();
  
  const requireJsonContent = () =&gt; {
    return (req, res, next) =&gt; {
      if (req.headers['content-type'] !== 'application/json') {
          res.status(400).send('Server requires application/json')
      } else {
        next()
      }
    }
  }
  
  app.get('/', (req, res, next) =&gt; {
    res.send('Welcome Home');
  });
  
  app.post('/', requireJsonContent(), (req, res, next) =&gt; {
    res.send('You sent JSON');
  })
  
  app.listen(3000);
  </code></pre>