Best way to serve coffeescript to client without connect-assets

Spend last few hours trying to figure out the best way to compile coffeescript and serve .js on request. I'm using require.js and no, coffee plugin for require is not the answer, just sounds odd... and I wouldn't want to use connect-assets because I'm using require.js. So I ended up writing something like:

app.configure 'development', ->
  app.get(/.js$/, (req, res, next) ->
    script = req.originalUrl.substring(1, req.originalUrl.length - 3)
    try
      file = "app/assets/javascripts/#{script}.coffee"
      cs = fs.readFileSync "#{__dirname}/#{file}", "ascii"
      try
        js = coffee.compile cs
        res.header 'Content-Type', 'application/x-javascript'
        res.send(js, 200)
      catch e
        res.send("Coffee compile error in #{file}\n" + e.stack, {'Content-Type': 'text/plain'}, 500)
    catch e
      next()
    )
    app.use(express.static("#{__dirname}/app/assets/javascripts"))

so I captures all .js requests, finds the corresponding .coffee files, compiles them and serves them back to client. BUT IT'S JUST TOO MUCH CODE.... Then I found coffee-middleware npm package and I thought it'll be cleaner to use just 3 lines of coffee to configure my server for coffee serving, but it just started compiling .js into same dir as coffee.

ANY HELP PLEASE. Any project samples I could look at on git or something? Thank you A LOT in advance!

Or I'll have to publish npm package of the code above, which I'm not sure if the best choice :)

Compile the coffee-script:

  • Use either the watch function of the coffee script compiler.
  • Create a Makefile (make coffee ;-)
  • Use grunt, or any other tool.

If you go with the Makefile approach you could make a target for starting a server, that compiles coffeescript before starting the server.

Then I found coffee-middleware npm package and I thought it'll be cleaner to use just 3 lines of coffee to configure my server for coffee serving, but it just started compiling .js into same dir as coffee.

connect-coffee-script module takes both src and dest options, so your source directory remains uncluttered.

Actually, I see coffee-middleware does too. It's right in the README.