codeforboston / ma-laws

The General Laws of the Commonwealth of Massachusetts
http://macode.org
Apache License 2.0
18 stars 3 forks source link

Error running locally #2

Closed alexshopov closed 11 years ago

alexshopov commented 11 years ago

As per our conversation at the meeting the other night, I installed rcouch, and successfully ran the Make to push laws onto my local server. When I point my browser to it, however, it's still rendering just the basic index.html with no styling, and when I click on either General or Session laws I get the following error in both Chrome and Firefox:

{"error":"illegal_databasename","reason":"Name: 'GeneralLaws'. Only lowercase characters (a-z), digits (0-9), and any of the characters , $, (, ), +, -, and / are allowed. Must begin with a letter."}

I'm sure I'm just missing a simple step somewhere. Thoughts?

calvinmetcalf commented 11 years ago

I think the page is set up to assume it's running on laws.calvinmetcalf.com I think I need to make a few changes to the base page

calvinmetcalf commented 11 years ago

ok so here's what your going to need to do, this line needs to be removed because it forces it to be on 'laws.calvinmetcalf.com' then you need to figure out a way to get it to proxy requests from a base url (like localhost:3000) to localhost:5984/law/_design/laws/_rewrite which is where the thing is running. If I can think of an easy way to do that I'll get back to you.

calvinmetcalf commented 11 years ago

I'm not going to be able to make it to tonight's meet up but I'm going to try to fix the app so it works on localhost

thadk commented 11 years ago

Would something like this help?

exports.proxy = function (req, res) {
  var httpProxy = require('http-proxy');
  var routingProxy = new httpProxy.RoutingProxy();

  routingProxy.proxyRequest(req, res, {
    target: {
      changeOrigin: true,
      host: process.env.HOSTBASE,
      port: process.env.HOSTPORT,
    }
  });
};
calvinmetcalf commented 11 years ago

would that though change the base path?

thadk commented 11 years ago

Maybe this one--as I recall, you just further modify the headers on the final request to have the base path you need?

exports.proxy = function (req, res) {
  var httpProxy = require('http-proxy');
  var routingProxy = new httpProxy.RoutingProxy();
  console.log(process.env.HOSTBASE);
  /* Heroku: Need this next line or "no such app" error from heroku */
  req.headers.host = process.env.HOSTBASE;

  /* Then modify the base path in the req.url before passing them onward */

  routingProxy.proxyRequest(req, res, {
    target: {
      changeOrigin: true,
      host: process.env.HOSTBASE,
      port: process.env.HOSTPORT,
    }
  });
};

And in the main app:

var localhostEnabler = require('localhostenabler'); 
app.get('/yourEndpoint/*', localhostEnabler.proxy);

If you need to tap into the external server's response, this hack worked for me: http://stackoverflow.com/questions/15235763/get-response-from-proxy-server/15240436#15240436

calvinmetcalf commented 11 years ago

@thadk thanks! @oceanadvocate I think these 2 lines are the only ones that use absolute urls so you can change the '/' to a '/law/_design/laws/_rewrite/' as well as possibly adding root:'/law/_design/laws/_rewrite/' after this line

thadk commented 11 years ago

Oh and I forgot to reference the other step: http://stackoverflow.com/questions/7561589/nodejs-proxy-not-responding/16153944#16153944 (thanks for the upvote?)

Use npm to install connect-restreamer and make this the final item in your app.configuration of express:

app.use(require('connect-restreamer')());
calvinmetcalf commented 11 years ago

ok finallly had some time to myself @oceanadvocate there is a branch now called site it works locally for me, it is also the start of my process of making it more app like by breaking things into separate files, requires browserify (npm install -g browserify also may have to do npm install inside the repo)

calvinmetcalf commented 11 years ago

I believe we solved this