What is the Meteor 3 Express replacement for Picker.middleware?

I could not find any example in the migration page and I tried the two variants below. None of them ran my middleware(s) for all http requests.

The second one did manage to install a router that worked fine when I registered a path with it. It’s just the middleware stuff (like json body parsing into req) that never runs.

Webapp.handlers.use(myMiddleware);
const router = Webapp.express.Router();
Webapp.handlers.use(router);
router.use(myMiddleware);
1 Like

Hey @permb, you are right. Docs aren’t clear yet.

The code below should work:

import { WebApp } from 'meteor/webapp';

const app = WebApp.express();
const router = WebApp.express.Router();

// This middleware is executed every time the app receives a request
router.use((req, res, next) => {
    console.log('Router-level - Time:', Date.now());
    next();
})

// This middleware shows request info for any type of HTTP request to the /hello/:name path
router.use('/hello/:name', (req, res, next) => {
    console.log('Router-level - Request URL:', req.originalUrl);
    next();
}, (req, res, next) => {
    console.log('Router-level - Request Type:', req.method);
    next();
})

// mount the router on the app
app.use('/', router);

WebApp.handlers.use(app);
1 Like

I just improved the webapp topic adding new examples and some additional details.

4 Likes