2

我正在使用 restify node.js 编写一个 REST API。

现在我正在这样做:

"http://test.com/products/query/keyword"  

Routing = server.get('products/query/:keyword', myCallBack);

并获得类似的参数req.pramams.keyword

我想获得如下参数:

"http://test.com/products/?query=keyword"
 Routing ?

提前致谢。

4

1 回答 1

4

您可以使用该req.query对象:

// if you use Express (as your tags seem to suggest)
var express = require('express');
var app     = express();

app.get('/products/', function(req, res) {
  res.send('Query sent: ' + req.query.query);
});

app.listen(3012);
// if you use Restify (as your text seems to suggest)
var restify = require('restify');
var app     = restify.createServer();

app.pre(restify.pre.sanitizePath()); // necessary to be able to use /products/
                                     // (with trailing slash)
app.use(restify.queryParser());

app.get('/products/', function(req, res) {
  res.send(req.query.query);
});

app.listen(3012);
于 2013-06-04T19:57:54.290 回答