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.