如何将stringvariables作为parameter passing给使用节点js的REST API调用

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

我需要使用string参数vm/hpcloud/nova/standard.small调用上面的API。 请注意, vm/hpcloud/nova/standard.small是单个string参数。

假设node.js和express.js。

用您的应用程序注册路线。

server.js:

 ... app.get('/myservice/:CustomerId', myservice.queryByCustomer); .... 

使用传入的ID中的req.params实现服务。

路线/ myservice.js:

 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... }); }; 

在你的app.js上:

 url: http://localhost:3000/params?param1=2357257&param2=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 

将您的url编码为参数:

VM%2Fhpcloud%2Fnova%2Fstandard.small

使用过的网站: http : //meyerweb.com/eric/tools/dencoder/

你可能正在寻找这个: http : //expressjs.com/api.html#res.json

所以它会

 res.json(pricingDetail); 

不使用string,如果你需要得到这个使用和id或可读的string,'/path/我的文章'不'/path/我的文章'