DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 968,873 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for Getting and setting headers in Node.js HTTP server
Onelinerhub
Onelinerhub

Posted on

Getting and setting headers in Node.js HTTP server

1. How to get request header in HTTP server

const http = require('http');

const server = http.createServer((req, res) => {
  res.end( req.headers['x-test'] );
});

server.listen(82, '127.0.0.1');
Enter fullscreen mode Exit fullscreen mode
  • require('http') - import module to work with http protocol,
  • http.createServer - creates HTTP server,
  • (req, - object with request data,
  • res - object to manage response,
  • .end( - finished http response with given content,
  • req.headers - object with all request headers,
  • x-test - sample request header to get value of.

Open original or edit on Github.

2. How to set response header in HTTP server

const http = require('http');

const server = http.createServer((req, res) => {
  res.setHeader('Content-Type', 'text/plain')
  res.end();
});

server.listen(82, '127.0.0.1');
Enter fullscreen mode Exit fullscreen mode
  • require('http') - import module to work with http protocol,
  • http.createServer - creates HTTP server,
  • (req, - object with request data,
  • res - object to manage response,
  • .end( - finished http response with given content,
  • .setHeader( - sets response header,
  • Content-Type - name of the header to send,
  • text/plain - value of the header to send.

Open original or edit on Github.

3. How to set content type in HTTP server

const http = require('http');

const server = http.createServer((req, res) => {
  res.setHeader('Content-Type', 'text/csv')
  res.end("1,2,3");
});

server.listen(82, '127.0.0.1');
Enter fullscreen mode Exit fullscreen mode
  • require('http') - import module to work with http protocol,
  • http.createServer - creates HTTP server,
  • (req, - object with request data,
  • res - object to manage response,
  • .end( - finished http response with given content,
  • .setHeader( - sets response header,
  • Content-Type - we're going to set content type,
  • text/csv - let's send csv content as example.

Open original or edit on Github.

4. How to allow CORS in HTTP server

const http = require('http');

const server = http.createServer((req, res) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
  res.setHeader('Access-Control-Max-Age', 60*60*24*30);

  res.end('Hi');
});

server.listen(82);
Enter fullscreen mode Exit fullscreen mode
  • require('http') - import module to work with http protocol,
  • http.createServer - creates HTTP server,
  • (req, - object with request data,
  • res - object to manage response,
  • setHeader( - sets response header,
  • Access-Control- - CORS headers to set,
  • '*' - allow access from all origins,
  • 60*60*24*30 - ACL lifetime is set to 30 days.

Open original or edit on Github.

Top comments (0)

Need a better mental model for async/await?

Check out this classic DEV post on the subject.

⭐️🎀 JavaScript Visualized: Promises & Async/Await

async await