简体   繁体   English

如何使用 Java/Spring Boot function 作为 url 在 Node.js 应用程序中执行 fetch 调用?

[英]How can I perform a fetch call in a Node.js app with a Java/Spring Boot function as the url?

I'm completely new to Node and Spring Boot and am having a difficult time performing a fetch call in my Node.js app.我对 Node 和 Spring Boot 完全陌生,并且在我的 Node.js 应用程序中执行 fetch 调用时遇到了困难。 The overall structure of the project is a React front-end with a Java/Spring Boot back-end and Gradle.项目的整体结构是一个 React 前端,一个 Java/Spring Boot 后端和 Gradle。 I'm trying to create a service that automatically runs in the background that the user will never interact with or even know of its existence.我正在尝试创建一个在后台自动运行的服务,用户永远不会与之交互甚至不知道它的存在。

I'm using Google BigQuery for this task and am running some simple SQL queries using the Node.js client library that Google provides.我正在使用 Google BigQuery 执行此任务,并且正在使用 Google 提供的Node.js 客户端库运行一些简单的 SQL 查询。 My issue is that after pulling in my data from BigQuery, I want to take that information and perform a POST call using fetch.我的问题是,在从 BigQuery 中提取数据后,我想获取该信息并使用 fetch 执行 POST 调用。 However, this requires a Java function to make use of this external service I'm trying to trigger with my POST call and when I try running node GetBigQuery.mjs in my terminal I get an error message:但是,这需要 Java function 来使用这个外部服务,我试图通过我的 POST 调用触发,当我尝试在终端中运行node GetBigQuery.mjs时,我收到一条错误消息:

TypeError: Failed to parse URL from /api/delayorders

[cause]: TypeError [ERR_INVALID_URL]: Invalid URL

input: '/api/delayorders', code: 'ERR_INVALID_URL'

I'm not using node-fetch, axios, or any external library to make the POST request as I'm running node 18.8.0 that comes built-in with fetch.我没有使用 node-fetch、axios 或任何外部库来发出 POST 请求,因为我正在运行内置 fetch 的节点 18.8.0。

Three main files in total:总共三个主要文件:

BigQuery.js BigQuery.js

Includes boilerplate code copied and pasted from Google's documentation.包括从 Google 文档中复制和粘贴的样板代码。


GetBigQuery.mjs获取BigQuery.mjs

// There is more code above and below but it's not
// necessary in order to understand my question

value.forEach(element => {
    fetch("/api/delayorders", {
        method: "POST",
        body: JSON.stringify({
            "orderIds": element.OrderReferenceId,
            "date": tomorrow,
            "time": "12:00",
            "minutesFromUTC": new Date().getTimezoneOffset(),
            "buCode": element.StoreNo,
        }),
        headers: {
            "Content-Type": "application/json",
        }
    }).then(response => {
        console.log(response);
    })
})

Delay.java延迟.java

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@Log4j2
@RestController
@AllArgsConstructor
@RequestMapping("api")

public class Delay {

    @Getter
    private final OrderDelayService orderDelayService;

    @PostMapping("/delayorders")
    @ResponseStatus(HttpStatus.OK)
    public String delayOrders(@RequestBody OrderDelayDto orderDelayDto) throws Exception {
        orderDelayService.delayOrders(orderDelayDto.getOrderIds(), orderDelayDto.getDate(), orderDelayDto.getTime(), orderDelayDto.getMinutesFromUTC(), orderDelayDto.getBuCode());
    return "OK";
    }
}

When you make an HTTP request in a browser, you can use a relative URL.当您在浏览器中发出 HTTP 请求时,您可以使用相对的 URL。 The absolute URL will be computed using the URL of the HTML document as the base URL.绝对值 URL 将使用 HTML 文档的 URL 作为基础 ZE6B3903238D2C85D87A 来计算。

So if you had an HTML document at http://example.com/ and make an HTTP request to /foo then the request would go to http://example.com/foo . So if you had an HTML document at http://example.com/ and make an HTTP request to /foo then the request would go to http://example.com/foo .

When your code is running in Node.js, there is no HTML document.当您的代码在 Node.js 中运行时,没有 HTML 文档。 There is no base URL.没有基础 URL。 There is no automatic conversion of a relative URL to an absolute one.相对 URL 不会自动转换为绝对值。

You need to use an absolute URL.您需要使用绝对的 URL。 (Or a mechanism that supports relative URLs and lets you specify the base URL). (或支持相对 URL 并允许您指定基本 URL 的机制)。

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

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