Skip to content

Commit 984f7a0

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

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

doc/api/http.md

+31
Original file line numberDiff line numberDiff line change
@@ -2693,6 +2693,37 @@ Returns a new instance of [`http.Server`][].
26932693
The `requestListener` is a function which is automatically
26942694
added to the [`'request'`][] event.
26952695

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

0 commit comments

Comments
 (0)