var express = require('express');
var app = express();
// Get Pricing details from subscription
app.get('/billingv2/resourceUri/:resourceUri', function(req, res) {
var pricingDetail = {}
pricingDetail.resourceUri = req.params.resourceUri;
pricingDetail.chargeAmount = '25.0000';
pricingDetail.chargeAmountUnit = 'per hour';
pricingDetail.currencyCode = 'USD';
res.send(pricingDetail); // send json response
});
app.listen(8080);
我需要使用字符串参数vm/hpcloud/nova/standard.small
调用上面的API。
请注意vm/hpcloud/nova/standard.small
是一个字符串参数。
答案 0 :(得分:8)
假设node.js和express.js。
在您的申请中注册路线。
...
app.get('/myservice/:CustomerId', myservice.queryByCustomer);
....
使用req.params
为传入的ID。
exports.queryByCustomer = function(req, res) {
var queryBy = req.params.CustomerId;
console.log("Get the data for " + queryBy);
// Some sequelize... :)
Data.find({
where : {
"CustomerId" : parseInt(queryBy)
}
}).success(function(data) {
// Force a single returned object into an array.
data = [].concat(data);
console.log("Got the data " + JSON.stringify(data));
res.send(data); // This should maybe be res.json instead...
});
};
答案 1 :(得分:2)
在你的app.js上:
url: http://localhost:3000/params?param1=2357257¶m2=5555
var app = express();
app.get('/params', function (req,res) {
// recover parameters
var param1=req.query.param1;
var param2=req.query.param2;
// send params to view index.jade
var params = {
param1: param1,
param2: param2
};
res.render('index.jade', {parametros: parametros});
});
在index.jade处恢复值:
p= params.param1
p= params.param2
答案 2 :(得分:1)
答案 3 :(得分:1)
答案 4 :(得分:0)
不使用字符串,如果你需要这个用途和id或可读的字符串,' / path / my-article'不是' / path /我的文章'