gpt4 book ai didi

javascript - Nodejs : make a GET request with users parameters

转载 作者:太空宇宙 更新时间:2023-11-04 00:38:22 24 4
gpt4 key购买 nike

我是 Nodejs 新手。我正在制作一个快速服务器。我通过 url 接收一些参数,并且我想要使用接收到的 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

undefined

{ parameters: { pattern: 'OEE' } }

OEE

Buffer 5b 5d

它应该看起来像:

node servicio.js

Servidor corriendo en: http:// localhost:3000

get_metric_names_by_pattern /get_metric_names_by_pattern/?pattern=OEE

OEE

undefined

{ parameters: { pattern: 'OEE' } }

OEE

Buffer 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 .. .

拜托,我需要知道如何将该参数插入到新网址中。如果这篇文章太长或太明显,我很抱歉。提前致谢

最佳答案

根据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){

});

关于javascript - Nodejs : make a GET request with users parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37925865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com