From 0d21a5748b4af0fcb2ab4f5276c471560fde8e5b Mon Sep 17 00:00:00 2001 From: Steven Chim <655241+chimurai@users.noreply.github.com> Date: Sat, 18 May 2024 23:23:14 +0200 Subject: [PATCH] docs(README): express request and response generics example (#1006) --- README.md | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 3e59082b..a66df5f2 100644 --- a/README.md +++ b/README.md @@ -26,48 +26,29 @@ Proxy `/api` requests to `http://www.example.org` :bulb: **Tip:** Set the option `changeOrigin` to `true` for [name-based virtual hosted sites](http://en.wikipedia.org/wiki/Virtual_hosting#Name-based). -```javascript -// javascript - -const express = require('express'); -const { createProxyMiddleware } = require('http-proxy-middleware'); - -const app = express(); - -app.use( - '/api', - createProxyMiddleware({ - target: 'http://www.example.org/secret', - changeOrigin: true, - }), -); - -app.listen(3000); - -// proxy and change the base path from "/api" to "/secret" -// http://127.0.0.1:3000/api/foo/bar -> http://www.example.org/secret/foo/bar -``` - ```typescript // typescript import * as express from 'express'; -import { createProxyMiddleware, Filter, Options, RequestHandler } from 'http-proxy-middleware'; +import type { Request, Response, NextFunction } from 'express'; + +import { createProxyMiddleware } from 'http-proxy-middleware'; +import type { Filter, Options, RequestHandler } from 'http-proxy-middleware'; const app = express(); -app.use( - '/api', - createProxyMiddleware({ +const proxyMiddleware = createProxyMiddleware({ target: 'http://www.example.org/api', changeOrigin: true, }), -); + +app.use('/api', proxyMiddleware); app.listen(3000); // proxy and keep the same base path "/api" // http://127.0.0.1:3000/api/foo/bar -> http://www.example.org/api/foo/bar + ``` _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#options) can be used, along with some extra `http-proxy-middleware` [options](#options).