├── .eslintignore
├── renovate.json
├── jest.config.js
├── .eslintrc
├── test
├── fixture
│ ├── api
│ │ └── index.js
│ ├── pages
│ │ └── index.vue
│ └── nuxt.config.js
└── module.test.js
├── .gitignore
├── tsconfig.json
├── .editorconfig
├── package.json
├── .github
└── workflows
│ └── ci.yml
├── LICENSE
├── src
├── index.ts
└── options.ts
├── README.md
└── CHANGELOG.md
/.eslintignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | dist
3 | .nuxt
4 | coverage
5 |
--------------------------------------------------------------------------------
/renovate.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": [
3 | "@nuxtjs"
4 | ]
5 | }
6 |
--------------------------------------------------------------------------------
/jest.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | preset: '@nuxt/test-utils'
3 | }
4 |
--------------------------------------------------------------------------------
/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "extends": [
3 | "@nuxtjs/eslint-config-typescript"
4 | ]
5 | }
6 |
--------------------------------------------------------------------------------
/test/fixture/api/index.js:
--------------------------------------------------------------------------------
1 | export default (req, res) => {
2 | res.end('url:' + req.url)
3 | }
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | *.iml
3 | .idea
4 | *.log*
5 | .nuxt
6 | .vscode
7 | .DS_Store
8 | coverage
9 | dist
10 |
--------------------------------------------------------------------------------
/test/fixture/pages/index.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 | Works!
4 |
5 |
6 |
7 |
12 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "target": "ESNext",
4 | "moduleResolution": "Node",
5 | "strict": true,
6 | "types": [
7 | "@nuxt/types"
8 | ]
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | # editorconfig.org
2 | root = true
3 |
4 | [*]
5 | indent_size = 2
6 | indent_style = space
7 | end_of_line = lf
8 | charset = utf-8
9 | trim_trailing_whitespace = true
10 | insert_final_newline = true
11 |
12 | [*.md]
13 | trim_trailing_whitespace = false
14 |
--------------------------------------------------------------------------------
/test/fixture/nuxt.config.js:
--------------------------------------------------------------------------------
1 | export default {
2 | createRequire: process.env.TEST ? 'native' : 'jiti',
3 | render: {
4 | resourceHints: false
5 | },
6 | modules: [
7 | '../../src/index.ts'
8 | ],
9 | serverMiddleware: {
10 | '/_api': '~/api/index'
11 | },
12 | proxy: {
13 | '/example': 'http://example.com'
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/test/module.test.js:
--------------------------------------------------------------------------------
1 | import { setupTest, url } from '@nuxt/test-utils'
2 | import fetch from 'node-fetch'
3 |
4 | describe('module', () => {
5 | setupTest({
6 | testDir: __dirname,
7 | fixture: 'fixture',
8 | server: true
9 | })
10 |
11 | test('object mode', async () => {
12 | expect(await fetch(url('/example')).then(r => r.text())).toMatch('Example Domain')
13 | })
14 | })
15 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "@nuxtjs/proxy",
3 | "version": "2.1.0",
4 | "description": "proxy support for nuxt server",
5 | "repository": "nuxt-community/proxy-module",
6 | "license": "MIT",
7 | "main": "dist/index.js",
8 | "types": "dist/index.d.ts",
9 | "files": [
10 | "dist"
11 | ],
12 | "scripts": {
13 | "build": "siroc build",
14 | "dev": "nuxt test/fixture",
15 | "lint": "eslint --ext .js,.vue,.ts .",
16 | "release": "yarn test && standard-version && git push --follow-tags && npm publish",
17 | "test": "yarn lint && jest"
18 | },
19 | "dependencies": {
20 | "http-proxy-middleware": "^1.0.6"
21 | },
22 | "devDependencies": {
23 | "@babel/preset-typescript": "^7.12.7",
24 | "@nuxt/test-utils": "latest",
25 | "@nuxt/types": "latest",
26 | "@nuxtjs/eslint-config-typescript": "latest",
27 | "eslint": "latest",
28 | "jest": "latest",
29 | "nuxt": "^2.14.12",
30 | "siroc": "latest",
31 | "standard-version": "latest"
32 | },
33 | "publishConfig": {
34 | "access": "public"
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: ci
2 |
3 | on:
4 | push:
5 | branches:
6 | - master
7 | pull_request:
8 | branches:
9 | - master
10 |
11 | jobs:
12 | ci:
13 | runs-on: ${{ matrix.os }}
14 |
15 | strategy:
16 | matrix:
17 | os: [ubuntu-latest]
18 | node: [14]
19 |
20 | steps:
21 | - uses: actions/setup-node@v2
22 | with:
23 | node-version: ${{ matrix.node }}
24 |
25 | - name: checkout
26 | uses: actions/checkout@master
27 |
28 | - name: cache node_modules
29 | uses: actions/cache@v2
30 | with:
31 | path: node_modules
32 | key: ${{ matrix.os }}-node-v${{ matrix.node }}-deps-${{ hashFiles(format('{0}{1}', github.workspace, '/yarn.lock')) }}
33 |
34 | - name: Install dependencies
35 | if: steps.cache.outputs.cache-hit != 'true'
36 | run: yarn
37 |
38 | - name: Lint
39 | run: yarn lint
40 |
41 | - name: Test
42 | run: yarn jest
43 |
44 | - name: Coverage
45 | uses: codecov/codecov-action@v1
46 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) Nuxt Community
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/src/index.ts:
--------------------------------------------------------------------------------
1 | import { createProxyMiddleware } from 'http-proxy-middleware'
2 | import type { Module } from '@nuxt/types'
3 | import { HttpProxyOptions, getProxyEntries, NuxtProxyOptions } from './options'
4 |
5 | declare module '@nuxt/types' {
6 | interface Configuration {
7 | proxy?: NuxtProxyOptions
8 | }
9 | }
10 |
11 | const proxyModule: Module = function (options: HttpProxyOptions) {
12 | const nuxt: any = this.nuxt
13 |
14 | if (!nuxt.options.server || !nuxt.options.proxy) {
15 | return
16 | }
17 |
18 | // Defaults
19 | const defaults: HttpProxyOptions = {
20 | changeOrigin: true,
21 | ws: true,
22 | ...options
23 | }
24 |
25 | const proxyEntries = getProxyEntries(nuxt.options.proxy as NuxtProxyOptions, defaults)
26 |
27 | // Register middleware
28 | for (const proxyEntry of proxyEntries) {
29 | // https://github.com/chimurai/http-proxy-middleware
30 | this.addServerMiddleware({
31 | prefix: false, // http-proxy-middleware uses req.originalUrl
32 | handler: createProxyMiddleware(proxyEntry.context, proxyEntry.options)
33 | })
34 | }
35 | }
36 |
37 | // @ts-ignore
38 | proxyModule.meta = require('../package.json')
39 |
40 | export default proxyModule
41 |
--------------------------------------------------------------------------------
/src/options.ts:
--------------------------------------------------------------------------------
1 | import type { Filter, Options as HttpProxyOptions } from 'http-proxy-middleware'
2 | export type { Options as HttpProxyOptions } from 'http-proxy-middleware'
3 |
4 | export type ProxyContext = Filter | HttpProxyOptions
5 | export type ProxyEntry = { context: ProxyContext, options: HttpProxyOptions }
6 |
7 | export type ProxyOptionsObject = { [target: string]: HttpProxyOptions }
8 | export type ProxyOptionsArray = Array<[ProxyContext, HttpProxyOptions?] | HttpProxyOptions | string>
9 | export type NuxtProxyOptions = ProxyOptionsObject | ProxyOptionsArray
10 |
11 | export function getProxyEntries (proxyOptions: NuxtProxyOptions, defaults: HttpProxyOptions): ProxyEntry[] {
12 | const applyDefaults = (opts?: HttpProxyOptions) => ({ ...defaults, ...opts }) as HttpProxyOptions
13 | const normalizeTarget = (input: string | HttpProxyOptions) =>
14 | (typeof input === 'object' ? input : { target: input }) as HttpProxyOptions
15 |
16 | const proxyEntries: ProxyEntry[] = []
17 |
18 | if (!proxyOptions) {
19 | return proxyEntries
20 | }
21 |
22 | // Object mode
23 | if (!Array.isArray(proxyOptions)) {
24 | for (const key in proxyOptions) {
25 | proxyEntries.push({
26 | context: key,
27 | options: applyDefaults(normalizeTarget(proxyOptions[key]))
28 | })
29 | }
30 | return proxyEntries
31 | }
32 |
33 | // Array mode
34 | for (const input of proxyOptions) {
35 | if (Array.isArray(input)) {
36 | proxyEntries.push({
37 | context: input[0],
38 | options: applyDefaults(normalizeTarget(input[1] as string))
39 | })
40 | } else {
41 | proxyEntries.push({
42 | context: input,
43 | options: applyDefaults()
44 | })
45 | }
46 | }
47 | return proxyEntries
48 | }
49 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # @nuxtjs/proxy
2 |
3 | [![npm version][npm-version-src]][npm-version-href]
4 | [![npm downloads][npm-downloads-src]][npm-downloads-href]
5 | [![codecov][codecov-src]][codecov-href]
6 | [![license][license-src]][license-href]
7 |
8 | > Proxy support for Nuxt 2 server
9 |
10 | [📖 **Release Notes**](./CHANGELOG.md)
11 |
12 | ## Nuxt 3
13 |
14 | In Nuxt 3 you can make use of [Route Rules](https://nitro.unjs.io/guide/routing) to configure your proxies.
15 |
16 | ```ts
17 | export default defineNuxtConfig({
18 | routeRules: {
19 | '/proxy/example': { proxy: 'https://example.com' },
20 | '/proxy/**': { proxy: '/api/**' },
21 | }
22 | })
23 | ```
24 |
25 | ## Features
26 |
27 | ✓ Path rewrites
28 |
29 | ✓ Host based router (useful for staging/test)
30 |
31 | ✓ Logs / Proxy Events
32 |
33 | ✓ WebSockets
34 |
35 | ✓ Auth / Cookie
36 |
37 | ✓ ...See [http-proxy-middleware](https://github.com/chimurai/http-proxy-middleware) docs
38 |
39 | ⚠ Does not work with `nuxt generate` (see [static target](https://nuxtjs.org/docs/2.x/features/deployment-targets#static-hosting)).
40 |
41 | ## Setup
42 |
43 | 1. Add `@nuxtjs/proxy` dependency to your project
44 |
45 | ```bash
46 | yarn add @nuxtjs/proxy # or npm install @nuxtjs/proxy
47 | ```
48 |
49 | 2. Add `@nuxtjs/proxy` to the `modules` section of `nuxt.config.js`
50 |
51 | ```js
52 | {
53 | modules: [
54 | // Simple usage
55 | '@nuxtjs/proxy'
56 | ],
57 | proxy: {
58 | // see Proxy section
59 | }
60 | }
61 | ```
62 |
63 | - Define as many as proxy middleware you want in `proxy` section of `nuxt.config.js` (See [proxy](#proxy) section below)
64 |
65 | ## `proxy`
66 |
67 | You can provide proxy config using either object or array.
68 |
69 | ### Array Config
70 |
71 | You can use [shorthand syntax](https://github.com/chimurai/http-proxy-middleware#shorthand) to configure proxy:
72 |
73 | ```js
74 | {
75 | proxy: [
76 | // Proxies /foo to http://example.com/foo
77 | 'http://example.com/foo',
78 |
79 | // Proxies /api/books/*/**.json to http://example.com:8000
80 | 'http://example.com:8000/api/books/*/**.json',
81 |
82 | // You can also pass more options
83 | [ 'http://example.com/foo', { ws: false } ]
84 | ]
85 | }
86 | ```
87 |
88 | ### Object Config
89 |
90 | Keys are [context](https://github.com/chimurai/http-proxy-middleware#context-matching)
91 |
92 | ```js
93 | {
94 | proxy: {
95 | // Simple proxy
96 | '/api': 'http://example.com',
97 |
98 | // With options
99 | '/api2': {
100 | target: 'http://example.com',
101 | ws: false
102 | },
103 |
104 | // Proxy to backend unix socket
105 | '/api3': {
106 | changeOrigin: false,
107 | target: { socketPath: '/var/run/http-sockets/backend.sock' }
108 | }
109 | }
110 | }
111 | ```
112 |
113 | ## Default Options
114 |
115 | - `changeOrigin` and `ws` options are enabled by default.
116 |
117 | You can provide default options to all proxy targets by passing options to module options:
118 |
119 | ```js
120 | export default {
121 | modules: [
122 | // Disable ws option to all proxified endpoints
123 | ['@nuxtjs/proxy', { ws: false }]
124 | ],
125 | proxy: [
126 | 'http://example.com/foo',
127 | 'http://example.com:8000/api/books/*/**.json',
128 | ]
129 | }
130 | ```
131 |
132 | This will be similar to:
133 |
134 | ```js
135 | export default {
136 | modules: [
137 | '@nuxtjs/proxy',
138 | ],
139 | proxy: [
140 | ['http://example.com/foo', { ws: false }],
141 | ['http://example.com:8000/api/books/*/**.json', { ws: false }]
142 | ]
143 | }
144 | ```
145 |
146 | ## Development
147 |
148 | 1. Clone this repository
149 | 2. Install dependencies using `yarn install` or `npm install`
150 | 3. Start development server using `npm run dev`
151 |
152 | ## License
153 |
154 | [MIT License](./LICENSE)
155 |
156 | Copyright (c) Nuxt Community
157 |
158 |
159 | [npm-version-src]: https://img.shields.io/npm/v/@nuxtjs/proxy/latest.svg?style=flat-square
160 | [npm-version-href]: https://npmjs.com/package/@nuxtjs/proxy
161 |
162 | [npm-downloads-src]: https://img.shields.io/npm/dt/@nuxtjs/proxy.svg?style=flat-square
163 | [npm-downloads-href]: https://npmjs.com/package/@nuxtjs/proxy
164 |
165 | [codecov-src]: https://img.shields.io/codecov/c/github/nuxt-community/proxy-module.svg?style=flat-square
166 | [codecov-href]: https://codecov.io/gh/nuxt-community/proxy-module
167 |
168 | [license-src]: https://img.shields.io/npm/l/@nuxtjs/proxy.svg?style=flat-square
169 | [license-href]: https://npmjs.com/package/@nuxtjs/proxy
170 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4 |
5 | ## [2.1.0](https://github.com/nuxt-community/proxy-module/compare/v2.0.1...v2.1.0) (2020-12-14)
6 |
7 |
8 | ### Features
9 |
10 | * update module (typescript rewrite) ([c9dad3c](https://github.com/nuxt-community/proxy-module/commit/c9dad3c547759ca05413b6d85087e80e468297a5))
11 |
12 | ### [2.0.1](https://github.com/nuxt-community/proxy-module/compare/v2.0.0...v2.0.1) (2020-07-16)
13 |
14 |
15 | ### Bug Fixes
16 |
17 | * skip module for nuxt build (fixes [#74](https://github.com/nuxt-community/proxy-module/issues/74)) ([50db85e](https://github.com/nuxt-community/proxy-module/commit/50db85eb5dd76dd20fe146b282cc082de721663f))
18 |
19 | ## [2.0.0](https://github.com/nuxt-community/proxy-module/compare/v1.3.3...v2.0.0) (2020-06-03)
20 |
21 |
22 | ### ⚠ BREAKING CHANGES
23 |
24 | * Bumping major version as http-proxy-middleware may introduce some usage regressions
25 |
26 | ### Features
27 |
28 | * bump to 2.x ([57eb492](https://github.com/nuxt-community/proxy-module/commit/57eb492c42cea6b6e90e4348065da308274fbc8a))
29 | * upgrade to `http-proxy-middleware` 1.0.1 ([0277f6d](https://github.com/nuxt-community/proxy-module/commit/0277f6d0c753a73b2b4ab15223c1a5f9da0085ca))
30 |
31 |
32 | ### Bug Fixes
33 |
34 | * **module:** use logger ([#40](https://github.com/nuxt-community/proxy-module/issues/40)) ([6bfbcad](https://github.com/nuxt-community/proxy-module/commit/6bfbcade52c364647c26084eb66a16ff6a7c7e11))
35 |
36 | ## [1.3.3](https://github.com/nuxt-community/proxy-module/compare/v1.3.2...v1.3.3) (2019-03-02)
37 |
38 |
39 | ### Bug Fixes
40 |
41 | * **module:** generate warn if not SPA and increase coverage ([#31](https://github.com/nuxt-community/proxy-module/issues/31)) ([8baafc9](https://github.com/nuxt-community/proxy-module/commit/8baafc9))
42 |
43 |
44 |
45 | ## [1.3.2](https://github.com/nuxt-community/proxy-module/compare/v1.3.1...v1.3.2) (2019-02-18)
46 |
47 |
48 | ### Bug Fixes
49 |
50 | * skip generate warn for now because of SPA ([7b23097](https://github.com/nuxt-community/proxy-module/commit/7b23097))
51 |
52 |
53 |
54 |
55 | ## [1.3.1](https://github.com/nuxt-community/proxy-module/compare/v1.3.0...v1.3.1) (2018-10-30)
56 |
57 |
58 | ### Bug Fixes
59 |
60 | * add hint for "universal" mode only ([#10](https://github.com/nuxt-community/proxy-module/issues/10)) ([c78b122](https://github.com/nuxt-community/proxy-module/commit/c78b122))
61 |
62 |
63 |
64 |
65 | # [1.3.0](https://github.com/nuxt-community/proxy-module/compare/v1.2.4...v1.3.0) (2018-10-29)
66 |
67 |
68 | ### Features
69 |
70 | * upgrade dependencies ([331fa23](https://github.com/nuxt-community/proxy-module/commit/331fa23))
71 |
72 |
73 |
74 |
75 | ## [1.2.4](https://github.com/nuxt-community/proxy-module/compare/v1.2.3...v1.2.4) (2018-03-31)
76 |
77 |
78 |
79 |
80 | ## [1.2.3](https://github.com/nuxt-community/proxy-module/compare/v1.2.2...v1.2.3) (2018-03-31)
81 |
82 |
83 | ### Bug Fixes
84 |
85 | * hide debug logs ([66c1905](https://github.com/nuxt-community/proxy-module/commit/66c1905))
86 |
87 |
88 |
89 |
90 | ## [1.2.2](https://github.com/nuxt-community/proxy-module/compare/v1.2.1...v1.2.2) (2018-03-31)
91 |
92 |
93 |
94 |
95 | ## [1.2.1](https://github.com/nuxt-community/proxy-module/compare/v1.2.0...v1.2.1) (2018-03-31)
96 |
97 |
98 |
99 |
100 | # [1.2.0](https://github.com/nuxt-community/proxy-module/compare/v1.1.4...v1.2.0) (2018-03-31)
101 |
102 |
103 |
104 |
105 | ## 1.1.4 (2018-01-28)
106 |
107 |
108 |
109 |
110 | ## [1.1.3](https://github.com/nuxt/modules/compare/@nuxtjs/proxy@1.1.2...@nuxtjs/proxy@1.1.3) (2017-11-20)
111 |
112 |
113 |
114 |
115 | **Note:** Version bump only for package @nuxtjs/proxy
116 |
117 |
118 | ## [1.1.2](https://github.com/nuxt/modules/compare/@nuxtjs/proxy@1.1.1...@nuxtjs/proxy@1.1.2) (2017-09-05)
119 |
120 |
121 | ### Bug Fixes
122 |
123 | * **proxy:** disable prefix ([3e2e70f](https://github.com/nuxt/modules/commit/3e2e70f))
124 |
125 |
126 |
127 |
128 |
129 | ## [1.1.1](https://github.com/nuxt/modules/compare/@nuxtjs/proxy@1.1.0...@nuxtjs/proxy@1.1.1) (2017-06-09)
130 |
131 |
132 | ### Bug Fixes
133 |
134 | * **plugin:** normalize array alternative form path ([811b5a9](https://github.com/nuxt/modules/commit/811b5a9))
135 |
136 |
137 |
138 |
139 |
140 | # 1.1.0 (2017-06-07)
141 |
142 |
143 | ### Bug Fixes
144 |
145 | * **proxy:** normalize target in object mode ([e9d4026](https://github.com/nuxt/modules/commit/e9d4026))
146 |
147 |
148 | ### Features
149 |
150 | * proxy module ([6dfca4d](https://github.com/nuxt/modules/commit/6dfca4d))
151 |
--------------------------------------------------------------------------------