linemanjs / lineman

Lineman helps you build fat-client JavaScript apps. It produces happiness by building assets, mocking servers, running specs on every file change
MIT License
1.18k stars 83 forks source link

mod_rewrite like behaviours #293

Closed TravisTheTechie closed 9 years ago

TravisTheTechie commented 9 years ago

TL;DR I Don't Know What I'm Doing(TM) and want mod_rewrite support

So I've moved stuff over to using lineman for production, everything is working great except in production we're using some mod_rewrite magic in apache to take /search to render /search.html (as well as a couple others). /search is just a Google CSE page, because we're lazy and it's all public content.

So I want to effectively do the same rewrite in development/locally but am have troubles.

So what's the way I should be doing this?

searls commented 9 years ago

This is something I have spent zero time on, but I imagine you could do a custom middleware that looked at the req path and then mapped it to an HTML file in generated/ and streamed that to res?

On Mon, Jul 7, 2014 at 2:00 PM, Travis Smith notifications@github.com wrote:

TL;DR I Don't Know What I'm Doing(TM) and want mod_rewrite support So I've moved stuff over to using lineman for production, everything is working great except in production we're using some mod_rewrite magic in apache to take /search to render /search.html (as well as a couple others). /search is just a Google CSE page, because we're lazy and it's all public content. So I want to effectively do the same rewrite in development/locally but am have troubles.

  • I tried just res.render('search.html') but it seems that's way more complicated and totally not what I want.
  • I tried importing https://www.npmjs.org/package/connect-modrewrite to no avail in server.js.
  • I tried res.location('/search.html'); next();; turns out that doesn't make any sense at all. It was late.
  • I finally got something sorta working via res.redirect('/search.html') but since there's a query string, I had to magic that in place to pass it along. This is far from ideal. So what's the way I should be doing this?

    Reply to this email directly or view it on GitHub: https://github.com/linemanjs/lineman/issues/293

davemo commented 9 years ago

Hi @TravisTheTechie, did you ever end up with a reasonable solution to this? If so, would you be willing to document it here for any future issue perusers? :)

TravisTheTechie commented 9 years ago

Sure, I just have...

    var staticPages = ["search", "help", "getting-started"];

    _.map(staticPages, function(page) {
      function handle(req, res) {
        fs.readFile('generated/' + page + '.html', function (err, data) {
          res.write(data);
          res.end();
        });
      }

      var url = '/' + page;

      app.get(url, handle);
      app.get(url + ".html", handle);
    });

In my server.js. I wouldn't call it ideal, but it's good enough for me.

davemo commented 9 years ago

Thanks! :)