diff options
author | Juan José Arboleda <soyjuanarbol@gmail.com> | 2021-07-20 00:44:29 +0300 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2021-07-28 23:35:40 +0300 |
commit | 984f7a0bf32f9e329d0b7975042f71212fa823c4 (patch) | |
tree | 838854a8980562635b7f8ddd8d27cb16884a2b74 /doc/api/http.md | |
parent | 3f0b62375b3a4edc8365b803749e8dd5abc706b0 (diff) |
doc: add code example to `http.createServer` method
PR-URL: https://github.com/nodejs/node/pull/39455
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'doc/api/http.md')
-rw-r--r-- | doc/api/http.md | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/doc/api/http.md b/doc/api/http.md index a3e68aee4c6..34cbf0a8e2b 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])` <!-- YAML |