9951 explained code solutions for 126 technologies


nodejsHow to set HTTP server requests timeout


const http = require('http');

const server = http.createServer((req, res) => {
  req.setTimeout(5 * 1000);
  res.end('Hi');
});

server.listen(82);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

req.setTimeout

sets request timeout

5 * 1000

timeout is set to 5 seconds (5000 milliseconds)