简体   繁体   English

如何在Node中的另一个端点上发出GET请求?

[英]How to make a GET request on another endpoint in Node?

I'd like to set up a Node server where inside one endpoint, a GET request can be made to another endpoint in the same server. 我想设置一个节点服务器,其中一个端点内可以向同一服务器中的另一个端点发出GET请求。

My code for testing this is as follows: 我的测试代码如下:

const express = require('express');
const app=express();
const https=require("https");

const port=1337;
app.listen(port,()=>{
    console.log('live on port '+port);
});



app.get('/',function(req,res){
    res.send('Homepage here');
    var url="localhost/test?param=abcde";
    https.get(url,res=>{
      body=JSON.stringify(res);
      console.log(body);
    });
});


app.get('/test',(req,res)=>{
    var response=req.param;
    console.log(response);
    res.send(response);
});

When the user goes to the main page, it should send a GET request to /test that returns the value specified by param . 当用户转到主页时,它应该向/test发送GET请求,该请求返回param指定的值。 However, when I open the page, I get this error at the console: 但是,当我打开页面时,在控制台上出现此错误:

Error: Unable to determine the domain name

This also happens if I set 如果我设置,也会发生这种情况

var url="./test?param=abcde";

or 要么

var url="/test?param=abcde";

What is the correct URL to use for accessing another endpoint in the same .js server file? 用于访问同一.js服务器文件中的另一个端点的正确URL是什么? And is this the correct way to pass a GET parameter into that endpoint and return a result to the calling endpoint? 这是将GET参数传递到该端点并将结果返回给调用端点的正确方法吗?

Note: it is a similar problem to this question , but that one doesn't seem to have a workable solution posted, unless there's something useful in this response that I'm overlooking. 注意:这是与此问题类似的问题,但是似乎没有发布可行的解决方案,除非此响应中有一些有用的东西被我忽略了。

I have three concerns. 我有三个问题。

First, port seems 1337, so shouldn't you request var url="localhost:1337/test?param=abcde"; 首先,端口似乎为1337,所以您不应该请求var url="localhost:1337/test?param=abcde"; instead of var url="localhost/test?param=abcde"; 而不是var url="localhost/test?param=abcde";

Second, do you use https or http ? 其次,您使用https还是http If http, use http module instead of https . 如果是http,请使用http模块而不是https

Third, it's GET request and ?param=abcde is a query parameter, so change to var response=req.query; 第三,它是GET请求, ?param=abcde是一个查询参数,因此更改为var response=req.query; instead of var response=req.param; 而不是var response=req.param; http://expressjs.com/ja/api.html#req.query http://expressjs.com/ja/api.html#req.query

I modified the code(in my local environment, this works). 我修改了代码(在我的本地环境中,此方法有效)。 I use http module because in my local environment https is not supported. 我使用http模块,因为在我的本地环境中不支持https But if you use twilio, you may use https 但是,如果您使用twilio,则可以使用https

const express = require('express');
const app=express();
const http=require("http");

const port=8080;
app.listen(port,()=>{
    console.log('live on port '+port);
});



app.get('/',function(req,res){
    res.send('Homepage here');
    var url="http://localhost:8080/test?param=abcde";
    var req = http.request(url,res=>{
      // comment out, because in my environment this causes error
      // body=JSON.stringify(res);
      // console.log(body);
      res.on('data', (chunk) => {
        console.log(`BODY: ${chunk}`);
      });
    });
    req.end();
});


app.get('/test',(req,res)=>{
    console.log('route /test')
    var response=req.query;
    console.log(response);
    res.send(response);
});

Output: 输出:

live on port 8080
route /test
{ param: 'abcde' }
BODY: {"param":"abcde"}

You should use http or https in first address 您应该在第一个地址中使用httphttps

var url="http://localhost/test?param=abcde"
// Or
var url="https://localhost/test?param=abcde"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM