Migration from Picker server-side routing to Webapp express router

I’m trying to migrate an app that uses Picker to Meteor 3.0 RC0 and need a little help. I haven’t been able to find any migration documentation yet.

For Picker in Meteor 2, installing a route looks like this:

Picker.route("/abc/:pathparameter",(req,res,next)=>{
...
})

The types for Express look a little different - params (path parameters like “pathparameter” above) are exposed as req.param and the query is exposed as req.query instead of living inside the params parameter.

I thought this would work in Meteor 3, but it doesn’t (“express.route is not a function”). What am I misunderstanding?

Webapp.connectApp.route("/abc/:pathparameter",(params,req,res,next)=>{
...
})

I had same problem and to see its here, please @meteorjscommunity to help.

Hey @permb, we have an as-yet-unreleased migration guide that should help you:

PS: I think the guide is good enough to be released @hschmaiske, @denyhs so that we can get feedback.

6 Likes

In Meteor 3 you are looking for the handler function and instead of route you put in the method, so it should be like this (still need to test this, I’m currently blocked by something else in my Meteor 3 upgrade):

WebApp.handlers.get(
  '/blog/:userId/rss',
  async (params: { userId?: string }, _: Request, res: Response) => {
    if (!params.userId) return return404(res)
    const data = await articlesData(BLOG_TYPES.USER, params.userId)
    if (!data) return return404(res)
    res.setHeader('content-type', 'application/rss+xml')
    res.writeHead(200)
    const rss = await buildRss(data)
    res.end(rss)
  }
)

Pretty much once you get to Webapp.handlers, you can switch to Express documentation.

Turns out my code was already sort of working, but there is one gotcha that migrators should be aware of:

Picker.useMiddleware registers middleware that will be run before any routing takes place regardless of whether a route was registered before the middleware.
Express will run middleware precisely depending on the order of registration.

When you instead use .use() from the Express APIs, the order of registration becomes important. If you register a route BEFORE you register middleware on the same express entity (application or router), the middleware will run AFTER the route handler.

1 Like