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

Viewed 26

I'm completely new to Node and Spring Boot and am having a difficult time performing a fetch call in my Node.js app. The overall structure of the project is a React front-end with a Java/Spring Boot back-end and 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. My issue is that after pulling in my data from BigQuery, I want to take that information and perform a POST call using fetch. 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:

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.

Three main files in total:

BigQuery.js

Includes boilerplate code copied and pasted from Google's documentation.


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

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";
    }
}
1 Answers

When you make an HTTP request in a browser, you can use a relative URL. The absolute URL will be computed using the URL of the HTML document as the base URL.

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. There is no base URL. There is no automatic conversion of a relative URL to an absolute one.

You need to use an absolute URL. (Or a mechanism that supports relative URLs and lets you specify the base URL).

Related