From a437de3c5f403562f27041f1aa81efbac234ba3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Arboleda?= Date: Mon, 19 Jul 2021 16:44:29 -0500 Subject: [PATCH] doc: add code example to `http.createServer` method PR-URL: https://github.com/nodejs/node/pull/39455 Reviewed-By: James M Snell --- doc/api/http.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/doc/api/http.md b/doc/api/http.md index 304c1397820a7d..b04890e97c4ce9 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -2626,6 +2626,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])`