DenoDDeno
Powered by
capo_grecoC
Denoβ€’4y agoβ€’
2 replies
capo_greco

serving two static folders and connecting to both with websockets

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
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
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,934Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Serving static website
Niranjan RameshNNiranjan Ramesh / help
3y ago
Serving multiple static files to an HTTP request
ZidanZZidan / help
3y ago
Compiling typescript frontend and serving it with Deno
[object Object][[object Object] / help
2y ago