javascript - 代理的 Grunt 问题 - Gruntjs

标签 javascript gruntjs

我整天都在努力在我的 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']);
};

当我运行我的 grunt 服务器 时,我只能访问我的代理。如果我尝试只点击代理以外的任何东西,我会得到一个 404。是什么给我这个问题?

最佳答案

我在使用 grunt-connect-proxy 设置代理时也遇到了很多麻烦.

挖掘 grunt-contrib-connect 的源代码,我意识到它使用了 nodeJs Connect幕后框架。

在内部 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;
}

这主要是将 connect.staticconnect.directory 中间件添加到传递给 connect(middlewares) 构造函数的数组中。

知道这一点,我们可以利用 proxy-middleware 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.
                ];
            }
        }
    }
}

基本上我们是在中间件数组中添加一个中间件。 这个新的代理中间件会将任何传入请求(如 http://localhost:9002/api/)转换为 http://localhost:8080/

关于javascript - 代理的 Grunt 问题 - Gruntjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20010335/

相关文章:

gruntjs - gruntfile 的 <%= yeoman.app %> 和 <%=yeoman.dist %> 变量

javascript - grunt 未定义 - AngularJs

javascript - Gruntfile.js 任务错误

javascript - 很好地使用 requirejs 或 grunt 与 concat/minification 来获得 angularjs 性能

javascript - 随机移动图像

javascript - 使用 JQuery 悬停缩放 div

javascript - 高效的 jquery 选择器

javascript - 我只想打印来自 HTML/JS 页面的特定 div 内容和电子邮件结果?

node.js - grunt构建动态jade页面

javascript - 在 jsdom 窗口中编辑元素并将窗口另存为新的 HTML 文件?