function createServer
Jump to headingcreateServer<Request extends IncomingMessage = IncomingMessage,Response extends ServerResponse = ServerResponse,>(requestListener?: RequestListener<Request, Response>): Server<Request, Response>
Returns a new instance of 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);
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);
Type Parameters Jump to heading
Jump to headingRequest extends IncomingMessage = IncomingMessage
Jump to headingResponse extends ServerResponse = ServerResponse
Parameters Jump to heading
optional
Jump to headingrequestListener: RequestListener<Request, Response>
Return Type Jump to heading
Jump to headingcreateServer<Request extends IncomingMessage = IncomingMessage,Response extends ServerResponse = ServerResponse,>(options: ServerOptions<Request, Response>,requestListener?: RequestListener<Request, Response>,): Server<Request, Response>
Type Parameters Jump to heading
Jump to headingRequest extends IncomingMessage = IncomingMessage
Jump to headingResponse extends ServerResponse = ServerResponse
Parameters Jump to heading
Jump to headingoptions: ServerOptions<Request, Response>
optional
Jump to headingrequestListener: RequestListener<Request, Response>