Search code examples
javascriptnode.jsubuntunext.jsnest

Function body.getReader() doesnt work on ubuntu


I'm trying to deploy a project on ubuntu server 22.04. Im using Next.js + Nest.js
Encountered a problem where my Fetch API works differently, on my localhost (macOS) everything works fine and i receive chunks of response, however when i deployed to ubuntu - im just receive the whole response straight away. Node version is 19.5 on both servers.
The only diff is that im using Https on prod server
Also Idk if it's connected or not, I receive 304 on my relogin POST request and 201 on the same request on my localhost

My frontend code:

try {
      const response = await fetch(
        `${domainConfig.BACKEND_DOMAIN_NAME}/v1/embedding/ask`,
        {
          method: 'POST',
          body: JSON.stringify(body),
          headers: {
            'Content-Type': 'application/json',
          },
        },
      );
      if (response.body) {
        const reader = response.body.getReader();
        // eslint-disable-next-line no-constant-condition
        while (true) {
          const { done, value } = await reader.read();
          if (done) {
            break;
          }
          const text = new TextDecoder().decode(value);
          setCurrentAnswer((prevState) => prevState + text);
        }
        setIsBotAnswering(false);
        setButtonLoading(false);
      }

My backend code:

import { Response } from 'express';
export const queryPineconeVectorStoreAndQueryLLM = async (
  ...,
  res: Response,
): Promise<void> => {

const result = await llm.call(
        prompt,
      undefined,
      [
        {
          handleLLMNewToken(token: string) {
            res.write(token);
          },
        },
      ],
    );
    }

Tried reinstalling node_modules and rebuilding & changing node version on ubuntu to 18. Didnt help at all.


Solution

  • The problem was in my nginx configuration. Needed to add one line of code

    proxy_buffering off;