Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda