Proxy passare multipart in NextJS(anche NodeJS)

0

Domanda

Il mio compito era quello di proxy pass multipart/form-data attraverso NextJS api rotte, ma il built-in bodyParser stata la rottura in arrivo muptipart dati. Tutti gli altri plugin per NodeJS non mi permette di proxy pass chiaro byte di multipart dati e altri oggetti che non è forma, dei dati.

Così come proxy passare multipart/form-data in NextJS API-percorsi senza plugin?

multipartform-data next.js node.js proxy
2021-11-23 17:03:25
1

Migliore risposta

0

Codice successivo è la soluzione per il successo proxy pass multipart/form-data senza plugin per NextJS:

// /pages/api/miltipart.ts

// helpers to generate cookies
import { setCookies } from '@utils/api';
import type { NextApiRequest, NextApiResponse } from 'next';

// turn off default parser for current route
export const config = {
  api: {
    bodyParser: false,
  },
};

const handler = async (req: NextApiRequest, res: NextApiResponse): Promise<void> => {
  // create container for all buffers of multipart/form-data
  const buffers: any[] = [];
  // first of all listen 'readable' event to catch all incoming request data
  req
    .on('readable', () => {
      // read every incoming chunk. Every chunk is 64Kb data of Buffer
      const chunk = req.read();
      if (chunk !== null) {
        buffers.push(chunk);
      }
    })
    // listen on end event of request to send our data
    .on('end', async () => {
        try {
          const result = await fetch('https://google.com/api/upload', {
            method: 'POST',
            credentials: 'include',
            mode: 'cors',
            headers: {
              'Content-Type': req.headers['content-type'] ?? 'multipart/form-data',
              'User-Agent': req.headers['user-agent'] ?? '',
              Authorization: 'Bearer Token',
            },
            // concatination of array of Buffers and store it to body
            body: Buffer.concat(buffers),
          });
          const body = await result.json();
          setCookies(res, result.headers);
          res.status(result.status).json(body);
          return;
        } catch (error) {
          res.status(500).json(error);
        }

      res.status(405);
      res.send('Method Not Allowed');
    });
};
export default handler;
2021-11-23 17:03:25

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................