title: How to set a custom timeout on http get nodeJS
body: <p>I can't find documentation on how to set a custom timeout for a given request in NodeJS (with Express) ? </p>
  
  <p>The following isn't working .... :</p>
  
  <pre><code>https.get("https://externalurl.com", { timeout: 1000 }, (res) =&gt; {
      resp.on("timeout", () =&gt; {
          console.log("Never fired");
      });
  });
  </code></pre>
  
  <p>Doing this doesn't work either :</p>
  
  <pre><code>https.get("https://externalurl.com", (req, res) =&gt; {
      req.setTimeout(1000);
  });
  </code></pre>
  
  <p>Doing this doesn't work either ...</p>
  
  <pre><code>https.get("https://externalurl.com", (res) =&gt; {
  
  })
  .setTimeout(1000);
  </code></pre>
  
  <p>It always waits more than 1 sec before throwing an error</p>
  
  <p>Can someone help ? Is there an "official" way to set a custom timeout for a given request ?</p>
  
  <p><strong>My full server.ts</strong></p>
  
  <pre><code>// Express server
  const app = express();
  
  const PORT = process.env.PORT || 80;
  const DIST_FOLDER = join(process.cwd(), "dist/browser");
  
  // * NOTE :: leave this as require() since this file is built Dynamically from webpack
  const {
    AppServerModuleNgFactory,
    LAZY_MODULE_MAP,
    ngExpressEngine,
    provideModuleMap
  } = require("./dist/server/main");
  
  // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
  app.engine(
    "html",
    ngExpressEngine({
      bootstrap: AppServerModuleNgFactory,
      providers: [provideModuleMap(LAZY_MODULE_MAP)]
    })
  );
  
  app.set("view engine", "html");
  app.set("views", DIST_FOLDER);
  
  // Example Express Rest API endpoints
  // app.get('/api/**', (req, res) =&gt; { });
  // Serve static files from /browser
  app.get(
    "*.*",
    express.static(DIST_FOLDER, {
      maxAge: "1y"
    })
  );
  
  // All regular routes use the Universal engine
  app.get("/", (req, res) =&gt; {
    res.render("index", { req });
  });
  
  app.get("/myCustomRoute", (req, res) =&gt; {
    const protocol: string = req.query.myUrl.split(":")[0];
    let httpProtocol;
  
    if (protocol === "http") {
      httpProtocol = require("http");
    } else if (protocol === "https") {
      httpProtocol = require("https");
    }
  
    try {
      // THIS IS THE REQUEST I WANT TO CUSTOMIZE THE TIMEOUT
      httpProtocol
        .get(
          `${req.query.myUrl}/custom/custom/custom/custom.php?param=${req.query.myParam}`,
          { rejectUnauthorized: false },
          (myRes) =&gt; {
            let data = "";
  
            // A chunk of data has been received.
            myRes.on("data", (chunk) =&gt; {
              data += chunk;
            });
  
            // The whole response has been received
            myRes.on("end", (resXml) =&gt; {
              switch (myRes.statusCode) {
                case 403:
                  res.status(403).send("Forbidden");
                  break;
                case 404:
                  res.status(404).send("Not found");
                  break;
                default:
                  res.send(data);
                  break;
              }
            });
          }
        )
        .on("error", (err) =&gt; {
          console.log(err);
          res.status(500).send("custom error");
        });
    } catch (e) {
      console.log(e);
      res.status(500).send("custom error");
    }
  });
  
  // Start up the Node server
  app.listen(PORT, () =&gt; {
    console.log(`Node Express server listening on http://localhost:${PORT}`);
  });
  </code></pre>