Skip to content

Commit 3ff5e15

Browse files
juanarboldanielleadams
authored andcommitted
doc: add code example to http.createServer method
PR-URL: #39455 Reviewed-By: James M Snell <[email protected]>
1 parent 110c088 commit 3ff5e15

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

doc/api/http.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,6 +2687,37 @@ Returns a new instance of [`http.Server`][].
26872687
The `requestListener` is a function which is automatically
26882688
added to the [`'request'`][] event.
26892689

2690+
```cjs
2691+
const http = require('http');
2692+
2693+
// Create a local server to receive data from
2694+
const server = http.createServer((req, res) => {
2695+
res.writeHead(200, { 'Content-Type': 'application/json' });
2696+
res.end(JSON.stringify({
2697+
data: 'Hello World!'
2698+
}));
2699+
});
2700+
2701+
server.listen(8000);
2702+
```
2703+
2704+
```cjs
2705+
const http = require('http');
2706+
2707+
// Create a local server to receive data from
2708+
const server = http.createServer();
2709+
2710+
// Listen to the request event
2711+
server.on('request', (request, res) => {
2712+
res.writeHead(200, { 'Content-Type': 'application/json' });
2713+
res.end(JSON.stringify({
2714+
data: 'Hello World!'
2715+
}));
2716+
});
2717+
2718+
server.listen(8000);
2719+
```
2720+
26902721
## `http.get(options[, callback])`
26912722
## `http.get(url[, options][, callback])`
26922723
<!-- YAML

0 commit comments

Comments
 (0)