988 字符长访问令牌导致 SyntaxError: Unexpected token ▼ in JSON at position 0 ... for JSON.parse() in proxyRes

问题描述

我得到了返回的 Nextjs API

res.status(200).json({ result: result })

结果是:

const result = {
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9",'expiry_date': 123132
}

我用 npm http-proxy proxyRes 截获响应:

        function interceptLoginResponse(proxyRes,req,res) {

            // Read the API's response body from
            // the stream:
            let apiResponseBody = []

            proxyRes.on('data',(chunk) => {
                // apiResponseBody += chunk
                apiResponseBody.push(chunk);
            })

            // Once we've read the entire API
            // response body,we're ready to
            // handle it:
            proxyRes.on('end',() => {

                try {

                    apiResponseBody = Buffer.concat(apiResponseBody).toString();

                    // Extract the authToken from API's response:
                    const { result } = JSON.parse(apiResponseBody)

                    console.log("authToken: ",result);

                    // Set the authToken as an HTTP-only cookie.
                    // We'll also set the SameSite attribute to
                    // 'lax' for some additional CSRF protection.
                    const cookies = new Cookies(req,res)

                    cookies.set('auth-token',result,{
                        httpOnly: true,sameSite: 'lax'
                    })

                    // Our response to the client won't contain
                    // the actual authToken. This way the auth token
                    // never gets exposed to the client.
                    res.status(200).json({ loggedIn: true })

                    resolve()

                } catch (err) {

                    reject(err)

                }

            })
        }

上述代码有效,因为我使用了 "access_token""eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9" 值 但是当我用 REAL access_token (eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIyIiwianRpIjoiM2VmogQyNWI5MWQ5ZGNiNzMyMjk0MGI0ZDc0NmI5MjBkMGI1MWMxMTI3YzM4YmM4OTg3YWUyMGY4MjVmZTA3ZDExNDEzOWRmZWZiYWU1NzciLCJpYXQiOiIxNjE4ODk2MzA3Ljg4MDgyNiIsIm5iZiI6IjE2MTg4OTYzMDcuODgwODI5IiwiZXhwIjoiMTY1MDQzMjMwNy44MzgyNDIiLCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.tyEV3_MbzLMR0oySeQwgwrc9kHjrvLLhO0cJhWO_u6_5MNe3DpxABWigzH02jhvBiqgz8qSulPMNI9jI4jmGsq6i5Fptv0gwnmD_DlvOaexgm7zOWy6Ui7HUCq6a9Lz1eXKOU-HCbkdYk2zmnQKMZLS9Zu3-STeIBT9u8vuQRPZmpJ6rySUT7apfANW14CvGJf2x-L2nIY5brz5UeYHqYmcRciknghsQEyI5iw-rftxkhW7pQeOFhGPyHR-iZuYCRV8SuDS_HKspEEIXyEp3jxkVeG3NBAp3tnFCtE9kmP4JxCkY-bUhco_FmNWSlXw1ealst-6u3E_ZW923fGATNoIW1MV5MbVpPOR74tF87IJeGAX3P7sXrQs6bLQ3doR91EMnSm9cKZToNL2IoV3wyZHUqFPoUVkNcBPxZCzN67JbdvnVoaLnLfBjiGZQonsaUurEcZJs1FRXhZCXAd_TJlI1qUCKDq4h1g8yV2lajJs55dNGTFAYx3aS5PvWJxwUMhlCQzvOkYTJhiCOH-MhQXRz9e7ruBQphxRuogFO4CcbMdEksiGhzMnPdUKLxq88tKJ2L-cR_igESjlHsa330aIgu-GST9Jk4CmGLLkpnZAmh0lJqAYn9_ftinEHs3J7SLPpET5FtIj-mp4otOzdCLbmGZRMfiZF4wZd8yafSXw) [ 988 个字符长 ]

替换它时

JSON.parse() 失败并显示错误

SyntaxError: Unexpected token ▼ in JSON at position 0
    at JSON.parse (<anonymous>)
    at IncomingMessage.<anonymous> (C:\xampp\htdocs\lounge-react\.next\server\pages\api\proxy\[...path].js:199:20)
    at IncomingMessage.emit (events.js:327:22)
    at endReadableNT (internal/streams/readable.js:1327:12)
    at processticksAndRejections (internal/process/task_queues.js:80:21)

我该如何解决这个问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)