Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!