Improve WebApp.connectHandlers.use documentation

Currently the docs contain the following example (docs and source):

/* global WebApp Assets */
import crypto from 'crypto'
import connectRoute from 'connect-route'

WebApp.connectHandlers.use(connectRoute(function (router) {
    router.get('/', function (req, res, next) {
        const buf = Assets.getText('index.html')

        if (buf.length > 0) {
            const eTag = crypto.createHash('md5').update(buf).digest('hex')

            if (req.headers['if-none-match'] === eTag) {
                res.writeHead(304, 'Not Modified')
                return res.end()
            }

            res.writeHead(200, {
                ETag: eTag,
                'Content-Type': 'text/html'
            })

            return res.end(buf);
        }

        return res.end('<html><body>Index page not found!</body></html>')
    })
}))

I do not see why connect-route gets pulled in. Can exactly the same result not be achieved without it and with less code? On top of that, connect-route has not had a commit in four years.

1 Like

Mmh, wait a minute. Does WebApp.connectHandlers.use work with request params (/a/b/c/:param)?

Personally I always hook in express. This also allows you to use all available express middleware and bypass the limitations (incl bad docs) for WebAppp.connectHandlers. Besides express is very light-weight.

const express = require('express')
const app = express()

app.get('some/route', (req, res, next) => { ... }

WebApp.connectHandlers.use(app)

No, it does not. The reason is that it uses connect underneath it. If you want to use it like that you need Picker

1 Like

@satya That looks cool. First cons I see is bundle size. Express is huge. I do not know how well your example can be tree shaken. Even if it only ends up in the server bundle, that could perhaps harm build time. I’ll see.

@storyteller Picker looks cool but I fear for introducing another legacy package. Are you maintaining it?

Express is server side so bundle size should usually not be important.

Picker has been forked under community packages, that fork has been updated and is being maintained by me.