Next.js 使用 http-proxy-middleware 进行评估返回 404

问题描述

在我的项目中,我有两个 lambda 函数一个用于 Nextjs 客户端数据,另一个用于 Node Express 服务器来处理 URL 路径。我使用这个 Node 应用程序通过代理 http-proxy-middleware 信息来显示正确的 Nextjs 页面内容

问题是当我在 localhost、端口 3000 上的 Nextjs 和 4001 上的 Proxy 中使用这两个函数时,我从 Nextjs 获得了正确的页面,但我收到的所有 _next/static 资产都是 404。Node Express 应用程序作为无服务器运行框架。

我的 Node Express 应用示例

const express = require('express');
const serverless = require('serverless-http');
const proxy = require('http-proxy-middleware');

const app = express()

app.use('/johndoe/86154_scid',proxy({
    target: 'http://localhost:3000/card?cardId=887340',changeOrigin: true,ignorePath: false,logLevel: 'debug',headers: {
        'Accept-Encoding': 'identity'
    },pathRewrite: {
        [`^/*`]: '',},onProxyRes: function OnProxyRes(proxyRes,req,res) {                           
        var chunked = /chunked/.test(proxyRes.headers["transfer-encoding"]);
        if (chunked) {
            proxyRes.headers["transfer-encoding"] = '';
            res.write = (function(override) {
                return function(chunk,encoding,callback) {
                    override.call(res,chunk,"binary",callback);
                };
            })(res.write);
            
            res.end = (function(override) {
                return function(chunk,callback);
                };
            })(res.end);
        }
    }
}))


module.exports.handler = serverless(app);

解决方法

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

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

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