9951 explained code solutions for 126 technologies


nodejsHow 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');ctrl + c
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