Sung Won Cho Sung Won Cho - 1 year ago 158
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.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
. But does WebApp allow you to access regular parameters?

Answer Source

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.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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download