Skip to content

Commit a437de3

Browse files
juanarboltargos
authored andcommitted
doc: add code example to http.createServer method
PR-URL: #39455 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent fdcc572 commit a437de3

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
@@ -2626,6 +2626,37 @@ Returns a new instance of [`http.Server`][].
26262626
The `requestListener` is a function which is automatically
26272627
added to the [`'request'`][] event.
26282628

2629+
```cjs
2630+
const http = require('http');
2631+
2632+
// Create a local server to receive data from
2633+
const server = http.createServer((req, res) => {
2634+
res.writeHead(200, { 'Content-Type': 'application/json' });
2635+
res.end(JSON.stringify({
2636+
data: 'Hello World!'
2637+
}));
2638+
});
2639+
2640+
server.listen(8000);
2641+
```
2642+
2643+
```cjs
2644+
const http = require('http');
2645+
2646+
// Create a local server to receive data from
2647+
const server = http.createServer();
2648+
2649+
// Listen to the request event
2650+
server.on('request', (request, res) => {
2651+
res.writeHead(200, { 'Content-Type': 'application/json' });
2652+
res.end(JSON.stringify({
2653+
data: 'Hello World!'
2654+
}));
2655+
});
2656+
2657+
server.listen(8000);
2658+
```
2659+
26292660
## `http.get(options[, callback])`
26302661
## `http.get(url[, options][, callback])`
26312662
<!-- YAML

0 commit comments

Comments
 (0)