Zappa ( Express JS ) - Configurable Path to Deliver Assets

163 views Asked by At

I am needing to respond to http get requests to serve assets. I am needing help to write a route that matches the following description.

Path Info

  • Has a configurable prefix ( basePath )
  • Has a segment that maps to a real file in the public folder ( path )
  • Form: [basePath]/[path]

Ex:

http://localhost:3000/app/collage/components/bootstrap/dist/css/bootstrap.min.css
basePath = '/app/collage' # set through CLI arguments when app loads
path = '/components/bootstrap/dist/css/bootstrap.min.css' # Comes from route

What I need:

I need to write a get method that will respond to the above type of URL, read the file and send it to the user. The following obviously doesn't work, but I guess now you know what I am asking for.

@get "#{settings.basePath}/:path", (req, res) ->
  res.sendFile __dirname + "public" + req.params.path

NOTE: The above is related to

Attempting to create configurable route for delivering assets.

1

There are 1 answers

0
Dream On

IIRC, Express's ":parameter" trick doesn't match slashes, so can't be used to match a "path". Just build your own regexp instead. ;o)

"IIRC", because I think I had the same problem. But, "use the source, Luke", right? I'm guessing the source (pun unintended) of this limitation is maybe line 300 of ./node_modules/express/lib/utils.js, in function exports.pathRegexp:

.replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function(_, slash, format, key, capture, optional, star){

Or maybe it's line 307:

(format && '([^/.]+?)' || '([^/]+?)')

where slashes are explicitly excluded?

Anyway, route parameters, assigned here to "key", are obviously matched by (\w+). I'm too lazy to figure what "capture" and "star" are used for. The API ref doc kind of sucks, I guess, so maybe you'd want to reverse engineer the code?

HTH?