Getting and setting headers in Node.js HTTP server
Tags: node, javascript, http
Published: 2022-08-04 13:52:24

## 1. How to get request header in HTTP server

```js
const http = require('http');

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

server.listen(82, '127.0.0.1');
```

- `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](https://onelinerhub.com/nodejs/how-to-get-request-header-in-http-server) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/nodejs/how-to-get-request-header-in-http-server.md).


## 2. How to set response header in HTTP server

```js
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');
```

- `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](https://onelinerhub.com/nodejs/how-to-set-response-header-in-http-server) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/nodejs/how-to-set-response-header-in-http-server.md).


## 3. How to set content type in HTTP server

```js
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');
```

- `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](https://onelinerhub.com/nodejs/how-to-set-content-type-in-http-server) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/nodejs/how-to-set-content-type-in-http-server.md).

## 4. How to allow CORS in HTTP server

```js
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);
```

- `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](https://onelinerhub.com/nodejs/how-to-allow-cors-in-http-server) or [edit on Github](https://github.com/Onelinerhub/onelinerhub/tree/main/nodejs/how-to-allow-cors-in-http-server.md).