grunt-middleware-proxy
raw JSON → 1.0.7 verified Sat Apr 25 auth: no javascript maintenance
grunt-middleware-proxy is a Grunt plugin that provides an HTTP/HTTPS proxy middleware for grunt-contrib-connect. It supports basic proxy, proxy over proxy, and proxy tunneling. Version 1.0.7 is the current stable release. The package has low maintenance (last commit 2020). It differs from alternatives by being tightly integrated with Grunt's connect task, allowing easy proxy configuration via the 'proxies' option. It uses the 'http-proxy' library under the hood but abstracts away details.
Common errors
error TypeError: grunt-middleware-proxy is not a function ↓
cause Requiring the main package directly instead of the Utils submodule.
fix
Change require('grunt-middleware-proxy') to require('grunt-middleware-proxy/lib/Utils').getProxyMiddleware()
error Error: connect.multipart() is not a function ↓
cause Incompatibility with newer versions of grunt-contrib-connect where the middleware API changed.
fix
Ensure you are using a compatible version of grunt-contrib-connect (e.g., ^0.11.0).
error Error: listen EADDRINUSE :::9000 ↓
cause Port conflict; usually another process is using the same port.
fix
Change the port in connect options or kill the process using the port.
Warnings
deprecated The package is in maintenance mode; last commit in 2020 and no recent updates. Consider using modern alternatives like http-proxy-middleware with express. ↓
fix Migrate to http-proxy-middleware or a similar actively maintained proxy middleware.
breaking The import path changed between versions: prior to 1.0.0, the module was at 'grunt-middleware-proxy' directly. In 1.0.0+, you must use 'grunt-middleware-proxy/lib/Utils'. ↓
fix Update your require/import to 'grunt-middleware-proxy/lib/Utils' and call .getProxyMiddleware().
gotcha The context path must start with '/' and should not end with '/'. Omitting these can cause unexpected routing behavior. ↓
fix Ensure context is e.g., '/api', not 'api' or '/api/'.
gotcha The host option must not include 'http://' or 'https://', just the domain. Including scheme will cause errors. ↓
fix Use host: 'example.com', not host: 'https://example.com'.
Install
npm install grunt-middleware-proxy yarn add grunt-middleware-proxy pnpm add grunt-middleware-proxy Imports
- getProxyMiddleware wrong
var getProxyMiddleware = require('grunt-middleware-proxy').getProxyMiddleware;correctvar getProxyMiddleware = require('grunt-middleware-proxy/lib/Utils').getProxyMiddleware; - ProxyMiddleware (default import) wrong
import proxyMiddleware from 'grunt-middleware-proxy';correctimport { getProxyMiddleware } from 'grunt-middleware-proxy/lib/Utils'; - grunt.loadNpmTasks wrong
require('grunt-middleware-proxy');correctgrunt.loadNpmTasks('grunt-middleware-proxy');
Quickstart
// Install
// npm install grunt-middleware-proxy --save-dev
// Gruntfile.js
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-middleware-proxy');
grunt.initConfig({
connect: {
server: {
options: {
port: 9000,
hostname: 'localhost',
middleware: function(connect, options, middlewares) {
var getProxyMiddleware = require('grunt-middleware-proxy/lib/Utils').getProxyMiddleware;
middlewares.unshift(getProxyMiddleware());
return middlewares;
}
},
proxies: [
{
context: '/api',
host: 'example.com',
port: 8080,
https: false,
rewriteHost: true
}
]
}
}
});
grunt.registerTask('default', ['connect:server']);
};