diff --git a/doc/api/http.md b/doc/api/http.md index a3e68aee4c6978..34cbf0a8e2b1ce 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -2693,6 +2693,37 @@ Returns a new instance of [`http.Server`][]. The `requestListener` is a function which is automatically added to the [`'request'`][] event. +```cjs +const http = require('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); +``` + +```cjs +const http = require('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); +``` + ## `http.get(options[, callback])` ## `http.get(url[, options][, callback])`