0

我在gulp-connect和之间进行通信时遇到问题gulp-stubby-server

gulp-connect正在端口 3006 上运行,而 stubby 正在端口 8000 上运行,但不知何故,它试图仅连接到端口 3006 上的 stubby,我收到 404 错误。我已经完成了代理中间件配置以将所有路径请求重定向/my-server到 stubby 服务器,但它似乎不起作用。我在这里想念什么?

angular.js:12185 POST http://localhost:3006/my-server/services/getTestData 404(未找到)

var configuration = {
    myProxy: _.extend(url.parse('http://localhost:8000/my-server/'), {route: '/my-server'})

}
gulp.task('static', function () {
    connect.server({
        root: ['target'],
        port: 3006,
        livereload: false,
        middleware: function () {
            return [
                proxy(configuration.myProxy)
            ];
        }
    });
});

gulp.task('stubby', function (cb) {
    var options = {
        files: [
            'mocks/test/*.{json,yaml,js}'
        ],
        callback: function (server, options) {
            server.get(1, function (err, endpoint) {
                if (!err) {
                    console.log(endpoint);
                }
            });
        },
        stubs: 8000,
        tls: 8443,
        admin: 8010

    };
    stubby(options, cb);
});

示例 mock.json

{
  "request" : {
    "url": "^/my-server/services/getTestData$",
    "method": "GET"
  },
  "response":{
    "status" : 200,
    "headers" :{
      "Content-Type" : "application/json"
    },
    "latency" : 1000,
    "body" : "Some Test Data"
  }

}

资源

var testResource = function($resource , CONS){
    console.log('test resource');

    var requestURL = '/my-server/services/getTestData';
    return $resource(requestURL , {},{
        getTestData: {
            method: 'POST'
        }
    });
};
4

1 回答 1

1

查看您发布的错误日志:

angular.js:12185 POST http://localhost:3006/my-server/services/getTestData 404(未找到)

这是一个POST请求,但您只GETmock.json文件中模拟了请求。您需要同时允许POSTGET请求:

{
  "request" : {
    "url": "^/my-server/services/getTestData$",
    "method": ["GET", "POST"]
  },
  "response":{
    "status" : 200,
    "headers" :{
      "Content-Type" : "application/json"
    },
    "latency" : 1000,
    "body" : "Some Test Data"
  }
}
于 2016-12-11T09:40:30.370 回答