On this page

    M

    http.createServer

    http.createServer(options?, requestListener?): http.Server
    Attributes
    options:Object
    connectionsCheckingInterval?:
    Sets the interval value in milliseconds to check for request and headers timeout in incomplete requests. Default: 30000.
    headersTimeout?:
    Sets the timeout value in milliseconds for receiving the complete HTTP headers from the client. See server.headersTimeout for more information. Default: 60000.
    highWaterMark?:number
    Optionally overrides all sockets' readableHighWaterMark and writableHighWaterMark. This affects highWaterMark property of both IncomingMessage and ServerResponse. Default: See stream.getDefaultHighWaterMark().
    httpValidation:string
    Controls how strictly HTTP header values are validated, both in incoming requests and in outgoing responses. Accepted values are:
    'strict':
    Rejects control characters other than horizontal tab, and characters outside the latin1 range.
    'relaxed':
    Also allows control characters other than NUL, CR, and LF, aligning with the Fetch specification.
    'insecure'?:
    Equivalent to insecureHTTPParser: true. Incoming requests are parsed with all --insecure-http-parser leniency flags, which disables their header value validation. Outgoing header values are validated as in 'relaxed' mode, so NUL, CR, and LF are still rejected. Cannot be used together with insecureHTTPParser. Default: 'strict'.
    insecureHTTPParser?:boolean
    If set to true, it will use an HTTP parser with leniency flags enabled. Using the insecure parser should be avoided. See --insecure-http-parser for more information. Default: false.
    IncomingMessage?:http.IncomingMessage
    Specifies the IncomingMessage class to be used. Useful for extending the original IncomingMessage. Default: IncomingMessage.
    joinDuplicateHeaders?:boolean
    If set to true, this option allows joining the field line values of multiple headers in a request with a comma (, ) instead of discarding the duplicates. For more information, refer to message.headers. Default: false.
    keepAlive?:boolean
    If set to true, it enables keep-alive functionality on the socket immediately after a new incoming connection is received, similarly on what is done in socket.setKeepAlive(). Default: false.
    keepAliveInitialDelay?:number
    If set to a positive number, it sets the initial delay before the first keepalive probe is sent on an idle socket. Default: 0.
    keepAliveTimeout?:
    The number of milliseconds of inactivity a server needs to wait for additional incoming data, after it has finished writing the last response, before a socket will be destroyed. See server.keepAliveTimeout for more information. Default: 65000.
    keepAliveTimeoutBuffer?:
    An additional buffer time in milliseconds added to keepAliveTimeout to extend the internal socket timeout. See server.keepAliveTimeoutBuffer for more information. Default: 1000.
    maxHeaderSize?:number
    Optionally overrides the value of --max-http-header-size for requests received by this server, i.e. the maximum length of request headers in bytes. Default: 16384 (16 KiB).
    noDelay?:boolean
    If set to true, it disables the use of Nagle's algorithm immediately after a new incoming connection is received. Default: true.
    requestTimeout?:
    Sets the timeout value in milliseconds for receiving the entire request from the client. See server.requestTimeout for more information. Default: 300000.
    requireHostHeader?:boolean
    If set to true, it forces the server to respond with a 400 (Bad Request) status code to any HTTP/1.1 request message that lacks a Host header (as mandated by the specification). Default: true.
    ServerResponse?:http.ServerResponse
    Specifies the ServerResponse class to be used. Useful for extending the original ServerResponse. Default: ServerResponse.
    shouldUpgradeCallback(request):Function
    A callback which receives an incoming request and returns a boolean, to control which upgrade attempts should be accepted. Accepted upgrades will fire an 'upgrade' event (or their sockets will be destroyed, if no listener is registered) while rejected upgrades will fire a 'request' event like any non-upgrade request. This options defaults to () => server.listenerCount('upgrade') > 0.
    uniqueHeaders:Array
    A list of response headers that should be sent only once. If the header's value is an array, the items will be joined using ; .
    rejectNonStandardBodyWrites?:boolean
    If set to true, an error is thrown when writing to an HTTP response which does not have a body. Default: false.
    optimizeEmptyRequests?:boolean
    If set to true, requests without Content-Length or Transfer-Encoding headers (indicating no body) will be initialized with an already-ended body stream, so they will never emit any stream events (like 'data' or 'end'). You can use req.readableEnded to detect this case. Default: false.
    requestListener:Function
    Returns:http.Server

    Returns a new instance of http.Server.

    The requestListener is a function which is automatically added to the 'request' event.

    import http from 'node:http';
    
    // Create a local server to receive data from
    const server = http.createServer((req, res) => {
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({
        data: 'Hello World!',
      }));
    });
    
    server.listen(8000);
    const http = require('node:http');
    
    // Create a local server to receive data from
    const server = http.createServer((req, res) => {
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({
        data: 'Hello World!',
      }));
    });
    
    server.listen(8000);
    import http from 'node:http';
    
    // Create a local server to receive data from
    const server = http.createServer();
    
    // Listen to the request event
    server.on('request', (request, res) => {
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({
        data: 'Hello World!',
      }));
    });
    
    server.listen(8000);
    const http = require('node:http');
    
    // Create a local server to receive data from
    const server = http.createServer();
    
    // Listen to the request event
    server.on('request', (request, res) => {
      res.writeHead(200, { 'Content-Type': 'application/json' });
      res.end(JSON.stringify({
        data: 'Hello World!',
      }));
    });
    
    server.listen(8000);