node.js - hapijs - serve static files from directory handler with variable path name -
i'm trying in routes file using hapijs + inert plugin
{ method: 'get', path: '/l/{path*}', handler: { directory: { path: (req) => { const defaultdir = '/public'; return gethostinfo(req.headers.host).then((d) => { if (!d || !d.directory) return defaultdir; return d.directory; }).catch((e) => { return defaultdir; }); } } } },
path
parameter expects string, array or function returns string or array...in case, function returns promise...so doesn't work. tried adding hapi-as-promised
package modifies reply
function support then
method did't work.
basically want serve static assets 1 directory or depending on host header value.
well, thing have in mind hack. first, have make extension point make async stuff:
server.ext({ type: `onprehandler`, method: (request, reply) => { const defaultdir = '/public'; return gethostinfo(request.headers.host).then((d) => { if (!d || !d.directory) { request.app.dirpath = defaultdir; return; } request.app.dirpath = d.directory; }).catch((e) => { request.app.dirpath = defaultdir; }).then(() => { return reply.continue(); }); } });
and route:
{ method: `get`, path: `/l/{path*}`, handler: { directory: { path: (request) => { return request.app.dirpath; } } } }
i don't know how right way, tested , worked. hope helps. , gotta notice, using node server files on production isn't common way reason.
Comments
Post a Comment