2

I've been trying to create api stubs for an app that was generated with yeoman. Nothing special here, I just called:

mkdir demo1 
cd demo1
yo angular

For dev purposes, I need api stubs, and the only module I've found so far that does this is lineman. However, lineman is not, as far as I can tell, very yeoman-friendly, so what I did was start lineman on port 8000, provide some api stubs (like the ones in docs), and added "grunt-connect-proxy": "~0.1.5" to project node modules, then created a proxy that wound pass all localhost:9000/api/* to localhost:8000/api/*, thus providing the stubs.

While this works, I'd like to remove the lineman dependency and provide the stubs myself. This is the code I've wrote so far (added express as dependency for routing/parameter parsing) - most of the code is bits & pieces from express modules, in order to wrap http.ServerRequest and http.ServerResponse into express request & response:

// load all grunt tasks
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

var drawRoutes = require('./config/server').drawRoutes;
var express = require('express');
var expressApp = express();
drawRoutes(expressApp);
var router = expressApp._router;

// Provide api stubs through express router functionality.
var apiStubSnippet = function(req, res, next) {
  var match = router.matchRequest(req);
  if (typeof(match) !== 'undefined') {
    // Found a match, invoke it
    if (match.callbacks.length > 0) {

      var _req = require('express/lib/request');
      var _resp = require('express/lib/response');

      // Set the two objects together ... but why?!
      _req.__proto__ = req;
      _req.app = expressApp;
      _req.res = _resp;

      _resp.__proto__ = res;
      _resp.app = expressApp;
      _resp.req = _req;

      match.callbacks[0](_req, _resp);
    }
  } else {
    // No match on the router, go next.
    next();
  }
};

At this point, calls seem to be resolved (no more errors in console). But, the calls stick to pending and I don't know how to proceed. I'm pretty sure the code is overly complex for what it needs to do, but I'm a beginner with grunt & node modules.

Any help would do, thanks.

4

2 回答 2

1

Angular 允许你存根$httpBackend,你试过吗?

我喜欢尽可能地打破 JS 最佳实践,所以这是我今年早些时候为一个项目所做的事情:

app.run(function ($httpBackend) {
  var createResponse = function (type, url, status, response) {
    var when = $httpBackend.when(type, url);
    if (angular.isDefined(status) && angular.isDefined(response))
      when.respond(status, response);
    return when;
  };

  String.prototype.get =
  RegExp.prototype.get = function (status, response) { return createResponse('GET', this, status, response); };

  String.prototype.post =
  RegExp.prototype.post = function (status, response) { return createResponse('POST', this, status, response); };

  String.prototype.put =
  RegExp.prototype.put = function (status, response) { return createResponse('PUT', this, status, response); };

  /^\/views\//
    .get()
    .passThrough();

  '/api/login'
    .post(200, {
      // response object.
    });
});

这种特殊的方法可能不是您想要的,但$httpBackend可能是。

于 2013-07-09T19:45:03.283 回答
0

这两个端口配置为相同的上下文,尝试更改为: localhost:9000/* 和 localhost:8000/api/* 。这些上下文和端口是在 Gruntfile 中配置的吗?

于 2013-10-01T00:49:53.023 回答