Skip to content

Commit 4fab094

Browse files
committed
docs(examples): add vanilla http server example
1 parent 639cfe4 commit 4fab094

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

examples/http-server/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Module dependencies.
3+
*/
4+
const http = require('node:http');
5+
const { createProxyMiddleware } = require('../../dist'); // require('http-proxy-middleware');
6+
7+
/**
8+
* Configure proxy middleware
9+
*/
10+
const jsonPlaceholderProxy = createProxyMiddleware({
11+
target: 'http://jsonplaceholder.typicode.com',
12+
changeOrigin: true, // for vhosted sites, changes host header to match to target's host
13+
logger: console,
14+
});
15+
16+
/**
17+
* Add the proxy to http-server
18+
*/
19+
const server = http.createServer(jsonPlaceholderProxy);
20+
21+
server.listen(3000);
22+
23+
console.log('[DEMO] Server: listening on port 3000');
24+
console.log('[DEMO] Opening: http://localhost:3000/users');
25+
26+
require('open')('http://localhost:3000/users');
27+
28+
process.on('SIGINT', () => server.close());
29+
process.on('SIGTERM', () => server.close());

recipes/servers.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Overview of `http-proxy-middleware` implementation in different servers.
44

55
Missing a server? Feel free to extend this list of examples.
66

7+
- [http.createServer](#httpcreateserver)
78
- [Express](#express)
89
- [Connect](#connect)
910
- [Next.js](#nextjs)
@@ -16,6 +17,27 @@ Missing a server? Feel free to extend this list of examples.
1617
- [grunt-browser-sync](#grunt-browser-sync)
1718
- [gulp-webserver](#gulp-webserver)
1819

20+
## http.createServer
21+
22+
Vanilla http server implementation with [`http.createServer`](https://nodejs.org/docs/latest/api/http.html#httpcreateserveroptions-requestlistener)
23+
24+
```javascript
25+
const http = require('node:http');
26+
const { createProxyMiddleware } = require('http-proxy-middleware');
27+
28+
/**
29+
* Configure proxy middleware
30+
*/
31+
const apiProxy = createProxyMiddleware({
32+
target: 'http://www.example.com',
33+
changeOrigin: true, // for vhosted sites, changes host header to match to target's host
34+
});
35+
36+
const server = http.createServer(jsonPlaceholderProxy);
37+
38+
server.listen(3000);
39+
```
40+
1941
## Express
2042

2143
https://github.com/expressjs/express

0 commit comments

Comments
 (0)