2

我正在使用grunt带有所有这些livereload插件的连接服务器。开发过程非常快,直到我添加了一些休息电话。我决定制作一些示例JSON文件,这些文件将作为休息电话的答案。

所以我需要将每个休息调用重定向static到某个文件夹(重要的是我会把它放在目标文件夹中)所以它会在Angular文件夹之外。

所以我需要一些plugin转接电话,例如:

http.get(localhost:port/x/y/name) to target/jsons_examples/x/y/name.json
http.get(localhost:port/z/name) to target/jsons_examples/z/name.json

grunt 文件:(1 个应用服务器,1 个模拟 Web 服务)

grunt.initConfig({
  connect: {
  all: {
    options: {
        port: 10100,
        hostname: "0.0.0.0",
        livereload: true
    }
  },
  webservices_mock: {
    options: {
        port: 8081,
        hostname: "0.0.0.0",
        middleware: function(connect, options, middlewares) {
            middlewares.unshift(function(req, res, next) {
                var pattern = new RegExp('\/rest\/[a-zA-Z0-9\/.]+', 'i'),
                    matches = req.url.match(pattern);

                if (!matches) {
                    return next();
                }

                req.setEncoding('utf8');
                res.writeHead(200, {"Content-Type": "application/json"});
                res.write(grunt.file.read(req.url.replace("/rest", "json_contracts") + "/example.json"));

                res.end();
            });

            return middlewares;
        }
    }
  }
},
...

现在我需要在 Web 服务模拟配置中将路径从 json_contracts 更改为 angular 文件夹之外的路径,例如:././././target/json_contracts

4

1 回答 1

1

您可以使用中间件选项来注入您自己的 URL 解析器。请参阅评论以了解其工作原理:

grunt.initConfig({
  connect: {
    all: {
      options:{
        port: 8080,
        base: dir_to_angular_main_folder
        hostname: "localhost",
        middleware: function(connect, options, middlewares) {
          middlewares.unshift(function(req, res, next) {
            // pattern is a RegExp which is going to find the redirected url
            var pattern = new RegExp('^/x/y/([0-9A-Z]+)$', 'i'),
                // matches is a result which is
                //  - undefined when no match found
                //  - an array with two values when found:
                //    0: the full string
                //    1: the string inside of the brackets (the file name)
                matches = req.url.match(pattern);

            // if your url does not match skip the bottom part
            if (!matches) return next();

            // this runs only if match is found, set up the redirect header
            // up to you to decide whether it is 301 or 302
            // I would keep it 302 for dev purposes (browsers won't cache it)
            res.writeHead(302, {
              Location: '/target/jsons_examples/x/y/' + matches[1] + '.json'
            });

            // send the response
            res.end();
          });

          return middlewares;
        }
      }
    }
  }
});

我想您仍然需要将模式更改为您需要的模式(x / y / target 听起来不像真名)。

有人可以说你可以更容易地做到这一点,而无需像这样匹配([0-9A-Z]+)并使用这里的匹配'/target/jsons_examples/x/y/' + matches[1] + '.json',他会是对的。之所以使用名称只是因为它更灵活,例如当您在 x 和 y 之间有一个文件夹等时。无论如何,正如之前所说,您也可以使其更简单,只需使用以下内容:

res.writeHead(302, {
  Location: '/target/jsons_examples' + req.url + '.json'
});

我无法测试它,也许它会给你一些错误,但我仍然希望这足以让你知道做什么和如何做。

编辑

好吧,根据您的 JSON 文件在网络服务器端不可见(根文件夹超出范围)这一事实,您可以进行某种url 重写而不是重定向:

var fs = require('fs');

grunt.initConfig({
  connect: {
    all: {
      options:{
        port: 8080,
        base: dir_to_angular_main_folder
        hostname: "localhost",
        middleware: function(connect, options, middlewares) {
          middlewares.unshift(function(req, res, next) {
            // pattern is a RegExp which is going to find the redirected url
            var pattern = new RegExp('^/x/y/([0-9A-Z]+)$', 'i'),
                // matches is a result which is
                //  - undefined when no match found
                //  - an array with two values when found:
                //    0: the full string
                //    1: the string inside of the brackets (the file name)
                matches = req.url.match(pattern);

            // if your url does not match skip the bottom part
            if (!matches) return next();

            // this runs only if match is found, set up the redirect header
            // reads the file content and sends as a response
            res.end(fs.readFileSync('/<some-absolute-path>/target/jsons_examples/x/y/' + matches[1] + '.json', 'utf8'));
          });

          return middlewares;
        }
      }
    }
  }
});

请注意,您需要在 Gruntfilefs顶部包含 Nodejs 标准模块才能使其工作。这又只是一个原型,根据您的要求更改路径。也许您还需要将 mime-type 作为响应的标头编写,关于如何在 Node 中执行此操作已经有很多答案。

于 2015-10-05T07:14:24.610 回答