Meteor WebApp 中间件:传递参数 [英] Meteor WebApp middleware: passing params
问题描述
我想知道在使用官方webapp包监听传入的HTTP时如何传递参数在特定路由上请求.
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.
这是一个示例代码:
WebApp.connectHandlers.use("/hello/:myParam", function(req, res, next) {
res.writeHead(200);
res.end("Your param is", req.myParam);
});
上述类似 Express 的示例不适用于 WebApp.经过一些实验,我现在知道我可以使用 req.query
访问查询参数.但是 WebApp 允许您访问常规参数吗?
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?
推荐答案
我不知道有这样的连接中间件(虽然它可能存在,在这种情况下你可以插入它),但它很容易复制该行为:
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]);
});
不太一样,但似乎运行良好.当然,大多数人只会将 Iron-router 用于此类用途,但我假设您出于某种原因想要避免这种情况.
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.
这篇关于Meteor WebApp 中间件:传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!