M
http.createServer
History
Added in: v0.1.13
v0.1.13
Introduced in: v0.10.0
v0.10.0
The
httpValidation option is supported now.v26.3.0
Add optimizeEmptyRequests option.
v25.1.0, v24.12.0
The
shouldUpgradeCallback option is now supported.v24.9.0, v22.21.0
The
highWaterMark option is supported now.v20.1.0, v18.17.0
The
requestTimeout, headersTimeout, keepAliveTimeout, and connectionsCheckingInterval options are supported now.v18.0.0
The
noDelay option now defaults to true.v18.0.0
The
noDelay, keepAlive and keepAliveInitialDelay options are supported now.v17.7.0, v16.15.0
The
insecureHTTPParser option is supported now.v13.8.0, v12.15.0, v10.19.0
The
maxHeaderSize option is supported now.v13.3.0
The
options argument is supported now.v9.6.0, v8.12.0
http.createServer(options?, requestListener?): http.Server
Attributes
options:
ObjectconnectionsCheckingInterval?:
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?:
numberOptionally overrides all
sockets'
readableHighWaterMark and writableHighWaterMark. This affects
highWaterMark property of both IncomingMessage and ServerResponse.
Default: See stream.getDefaultHighWaterMark().httpValidation:
stringControls HTTP header value validation strictness
for incoming requests. Accepted values are:
'strict':
Strictest validation; rejects any non-ASCII or control
characters in header values.
'relaxed':
Allows a limited set of non-ASCII characters in header
values, aligning with the
Fetch specification.
'insecure'?:
Disables all header value validation (equivalent to
insecureHTTPParser: true).
Cannot be used together with insecureHTTPParser. Default: 'strict'.insecureHTTPParser?:
booleanIf 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.IncomingMessageSpecifies the
IncomingMessage
class to be used. Useful for extending the original IncomingMessage.
Default: IncomingMessage.joinDuplicateHeaders?:
booleanIf 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?:
booleanIf 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?:
numberIf 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.maxHeaderSize?:
numberOptionally 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?:
booleanIf 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?:
booleanIf 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.ServerResponseSpecifies the
ServerResponse class
to be used. Useful for extending the original ServerResponse. Default:
ServerResponse.shouldUpgradeCallback(request):
FunctionA 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:
ArrayA 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?:
booleanIf set to
true, an error is thrown
when writing to an HTTP response which does not have a body.
Default: false.optimizeEmptyRequests?:
booleanIf 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:
FunctionReturns:
http.ServerReturns 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);