9951 explained code solutions for 126 technologies


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

we're going to set content type

text/csv

let's send csv content as example