D
Deno

help

serving two static folders and connecting to both with websockets

Ccapo_greco9/23/2022
A lot of the youtube and medium resources available on setting serving static folders, and on setting up a websockets server, seem to be using patterns that are not compatible with each other / not harmonious with the latest version of std etc. I have a few stumbling blocks. The first is to serve my 'public' folder to the url, and serve another folder, called 'control', to URL/control, but within the one serve () call. Using a switch statement seems to break everything and I don't really understand why. Eg,
const servePublic = req => staticFiles ('public') ({
request: req,
respondWith: r => r
})

serve (req => servePublic (req), { addr: ':80' })
const servePublic = req => staticFiles ('public') ({
request: req,
respondWith: r => r
})

serve (req => servePublic (req), { addr: ':80' })
... works, whereas:
// serve public
const servePublic = req => staticFiles ('public') ({
request: req,
respondWith: r => r
})

// serve control
const serveControl = req => staticFiles (`control`) ({
request: req,
respondWith: r => r
})

serve (req => {
const url = new URL(req.url);
console.log (url.pathname)

switch (url.pathname) {
case `/control`:
return serveControl (req)
case `/`:
return servePublic (req)
}
}, { addr: ':80' })
// serve public
const servePublic = req => staticFiles ('public') ({
request: req,
respondWith: r => r
})

// serve control
const serveControl = req => staticFiles (`control`) ({
request: req,
respondWith: r => r
})

serve (req => {
const url = new URL(req.url);
console.log (url.pathname)

switch (url.pathname) {
case `/control`:
return serveControl (req)
case `/`:
return servePublic (req)
}
}, { addr: ':80' })
... does not. I feel like I am missing something vital.
Then there is websockets - trying to work out how to marry Deno.upgradeWebSocket with this serve pattern. I had gotten them all working on localhost, but Deno Deploy needs for there to be only one listener, I think
UUUnknown User9/23/2022
2 Messages Not Public
Sign In & Join Server To View

Looking for more? Join the community!