Sung Won Cho Sung Won Cho - 2 months ago 22
Javascript Question

Meteor WebApp middleware: passing params

I would like to know how to pass parameters when using the official webapp package to listen to incoming HTTP requests on a particular route.

Here is an example code:

WebApp.connectHandlers.use("/hello/:myParam", function(req, res, next) {
res.writeHead(200);
res.end("Your param is", req.myParam);
});


The above Express-like example does not work with WebApp. After some experiments, I now know I can access query params using
req.query
. But does WebApp allow you to access regular parameters?

Answer

I don't know of a connect middleware that does that (it might exist though, in which case you could plug it in), but it's easy enough to replicate that behavior:

WebApp.connectHandlers.use("/hello/", function(req, res, next) {
    var parts = req.url.split("/");
    res.writeHead(200);
    res.end("Your param is " + parts[1]);
});

Not quite the same but seems to work well. Of course, most people would just use iron-router for something like this, but I'm assuming you want to avoid that for some reason.