Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
extending http.IncomingMessage and http.ServerResponse & providing it to the http.createServer . How to do that in TS?

I want to provide my own custom http.IncomingMessage and http.ServerResponse class to http.createServer(opt,fn) . I have been searching for about 2 hour to find a way to this in typescript . But it didn't help . But this same code works in JS . Please help me . I just want to provide a custom Request and Response Object to the http.createServer() .

Here is my server.js JAVASCRIPT file .

const { createServer, IncomingMessage, ServerResponse } = require("http");

class Request extends IncomingMessage {
  constructor(socket) {
    super(socket);
  }

  getMeAnything() {
    return "you got REQUEST";
  }
}

class Response extends ServerResponse {
  constructor(req) {
    super(req);
  }

  getMeAnything() {
    return "you got  RESPONSE";
  }
}

const server = createServer(
  {
    IncomingMessage: Request,
    ServerResponse: Response,
  },
  (req, res) => {
    console.log(req.getMeAnything());

    res.end("Hello How are you ?");
  }
);

server.listen(3000);

here is my server.ts TYPESRIPT file :

import { Socket } from "net";
import { createServer, IncomingMessage, ServerResponse } from "http";

class Req extends IncomingMessage {
  constructor(socket: Socket) {
    super(socket);
  }

  getMeAnything() {
    return "you got anything inside REQUEST";
  }
}

class Res extends ServerResponse {
  constructor(req: IncomingMessage) {
    super(req);
  }

  getMeAnything() {
    return "you got anything inside RESPONSE";
  }
}

const server = createServer(
  {
    IncomingMessage: Req,
    ServerResponse: Res,
  },
  (req: Req, res: Res) => {
  
    // here I get a big fat error . Some suggested me to write @ts-ignore . But I think there is a solution for it . 
    
    console.log(req.getMeAnything());

    res.end("Hello How are you ?");
  }
);

server.listen(3000);

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda