简体   繁体   English

代理问题-Gruntjs

[英]Grunt problems with proxies - Gruntjs

I have been working all day on trying to get my proxies set up in my Gruntfile. 我整天都在努力在我的Gruntfile中设置代理。 Here is my Gruntfile : 这是我的Gruntfile

var proxySnippet = require('grunt-connect-proxy/lib/utils').proxyRequest;

module.exports = function(grunt) {

  require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

  grunt.initConfig({
    connect:{
      livereload: {
        options: {
          middleware: function (connect) {
            return [proxySnippet];
          }
        }
      },
      options: {
        port: 9000,
        base: 'app',
        keepalive: true,
        livereload: true
      },
      proxies: [
        {
          context: '/public/api',
          host: 'localhost',
          port: 8182,
          https: false,
          rewrite: {
            '^/public/api': ''
          }
        }
      ]
    }
  });

  grunt.registerTask('server', ['less', 'configureProxies', 'connect', 'connect', 'watch', 'open:dev']);
};

When I run my grunt server I can only hit my proxy. 当我运行grunt server时,只能打我的代理服务器。 If I try to just hit anything other than the proxy I get a 404 . 如果我尝试打除代理服务器以外的任何东西,我会得到404 What is giving me this issue? 是什么给我这个问题?

I also had a lot of trouble setting up a proxy using grunt-connect-proxy . 使用grunt-connect-proxy设置代理服务器也很麻烦。

Digging in the source code of grunt-contrib-connect , I realized that it uses the nodeJs Connect framework behind the scene. 挖掘grunt-contrib-connect的源代码,我意识到它使用了幕后的nodeJs Connect框架。

Internally the middleware option defaults to this function: 在内部, middleware选项默认为该功能:

function (connect, options) {
    var middlewares = [];
    if (!Array.isArray(options.base)) {
        options.base = [options.base];
    }
    var directory = options.directory || options.base[options.base.length - 1];
    options.base.forEach(function (base) {
        // Serve static files.
        middlewares.push(connect.static(base));
    });
    // Make directory browse-able.
    middlewares.push(connect.directory(directory));
    return middlewares;
}

Which basically adds the connect.static and the connect.directory middlewares to an array passed to the connect(middlewares) constructor. 它基本上将connect.staticconnect.directory中间件添加到传递给connect(middlewares)构造函数的数组中。

Knowing that, we can make use of the proxy-middleware nodeJs package like this: 知道了这一点,我们可以使用代理中间件 nodeJs包,如下所示:

connect: {
    server: {
        options: {
            port: 9002,
            keepalive: true,
            middleware: function (connect, options) {
                // Proxy all requests to target the local application.
                var proxyOptions = require('url').parse('http://localhost:8080/');
                proxyOptions.route = '/api';
                return [
                    require('proxy-middleware')(proxyOptions), // Include the proxy first.
                    connect.static(options.base), // Serve static files.
                    connect.directory(options.base) // Make empty directories browse-able.
                ];
            }
        }
    }
}

Basically we are adding a middleware to the middleware array. 基本上,我们在中间件阵列中添加了中间件。 This new proxy middleware will translate any incoming request like http://localhost:9002/api/ into http://localhost:8080/ 这个新的代理中间件会将所有传入的请求(例如http://localhost:9002/api/转换为http://localhost:8080/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM