Nodejs:使用用户参数发出GET请求

时间:2016-06-20 15:06:29

标签: javascript node.js url express server

我是nodejs的新手。我正在制作快递服务器。我通过url查看了一些参数,并且我想要使用recived url中的参数向另一台服务器发出请求。

var express = require("express"),
app = express(),
bodyParser  = require("body-parser"),
methodOverride = require("method-override");
request = require('request');
Client = require('node-rest-client').Client;


app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(methodOverride());

var router = express.Router();

app.use(router);

router.get('/get_metric_names_by_pattern', function(req, res) {

   console.log('get_metric_names_by_pattern '+req.url);

   console.log(req.query.pattern);
   //console.log(req.parameters);

   res.header('Access-Control-Allow-Origin', '*');
   var client = new Client();
   var salida;


   console.log(req.params.name);
   //Creamos la variable para los parametros
   var args = {
        parameters: {pattern: "OEE"}
        //headers: { "test-header": "client-api" }
    };



    console.log(args);
    console.log(args.parameters.pattern);
   client.get('http:// localhost:8000/get_metric_names_by_pattern/?pattern=:pattern', req.query.pattern,


    //http://   localhost:8000/get_metric_names_by_pattern/?pattern=OEE

    function (data, response) {
        //console.log(args.parameters.pattern);
        console.log(data);
        //var pattern = req.query.pattern;
        //data = data.toString('utf8'); //Transformamos los datos a string para que los reconozca el navegador
        //console.log(data);
      salida = JSON.parse(data);
      //console.log("Datos:", salida.date);
      res.send(salida); //Lo enviamos al navegador
      //console.log(salida);
    });
});

我确实从客户端(在这种情况下' OEE')中找到了参数,但我发现无法将该字符串插入 client.get(' http :// localhost:8000 / get_metric_names_by_pattern /?pattern =:pattern',req.query.pattern,。

请求应该看起来像那个下面的注释行(// http://localhost:8000/get_metric_names_by_pattern/?pattern=OEE),并且从那个请求我重新编写一个json,就像这样:

["PanelPC0_OEE", "empacadora1.OEE", "empacadora1.OEEE", "empacadora1.OEE_Avaible", "empacadora1.OEE_Performance", "empacadora1.OEE_quality"] 

console.log看起来像这样:

  

node servicio.js

     

Servidor corriendo en:http:// localhost:3000

     

get_metric_names_by_pattern / get_metric_names_by_pattern /?pattern = OEE

     

OEE

     

未定义

     

{参数:{pattern:' OEE' }}

     

OEE

     

缓冲液5b 5d

它应该看起来像:

  

node servicio.js

     

Servidor corriendo en:http:// localhost:3000

     

get_metric_names_by_pattern / get_metric_names_by_pattern /?pattern = OEE

     

OEE

     

未定义

     

{参数:{pattern:' OEE' }}

     

OEE

     

缓冲液5b 22 50 61 6e 65 6c 50 43 30 5f 4f 45 45 22 2c 20 22 74 65 61 6d 66 6f   6f 64 73 2e 62 6f 67 6f 74 61 2e 6d 61 72 67 61 72 69 6e 61 73 2e 65 6d 70 61 ..   。

请,我需要知道如何将该参数插入新网址。对不起,如果这篇文章是长期或obvius。提前致谢

2 个答案:

答案 0 :(得分:1)

根据documentation,要插入参数或参数,您应该使用:

  

$ {PARAM}

就像这个例子:

var args = {
    path: { "id": 120, "arg1": "hello", "arg2": "world" },
    parameters: { arg1: "hello", arg2: "world" },
    headers: { "test-header": "client-api" }
};


client.get("http://remote.site/rest/json/${id}/method?arg1=${arg1}&arg2=${arg2}", args,
    function (data, response) {
        console.log(data);
        console.log(response);
    });

所以在你的情况下你应该尝试使用:

var args = {
    parameters: { pattern: req.query.pattern },
};

client.get('http:// localhost:8000/get_metric_names_by_pattern/?pattern=${pattern}', args,
    function(data, response){

    });

答案 1 :(得分:0)

虽然它不漂亮...... @mrwillihog的评论给了我这个想法,实际上有效!

soc