2013-06-04 61 views
2

我使用restify node.js編寫REST API。Node.js url路由格式化

現在我在做這樣的:

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

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

和獲取PARAMS像req.pramams.keyword

我想得到這樣PARAMS:

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

在此先感謝。

回答

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); 
+0

非常感謝...... :) – arnold