Node.js 为什么我的Express服务器响应仅在SSR期间截断?

Node.js 为什么我的Express服务器响应仅在SSR期间截断?,node.js,reactjs,express,nextjs,Node.js,Reactjs,Express,Nextjs,我正在使用Next.js、Redux和Express。我的页面包含以下代码: static async getInitialProps({store, isServer, pathname, query}) { const res = await fetch('http://localhost:3001/tutorials'); const tutorials = await res.json(); store.dispatch(tutorialsReceived({tutorial

我正在使用Next.js、Redux和Express。我的页面包含以下代码:

static async getInitialProps({store, isServer, pathname, query}) {
  const res = await fetch('http://localhost:3001/tutorials');
  const tutorials = await res.json();
  store.dispatch(tutorialsReceived({tutorials}));
}
我收到React debug错误,说我的服务器响应与我的客户端不同。它需要一个很长的JSON响应(252KB),而服务器渲染被过早地切断。我尝试了两种方式来发送文件,但不确定为什么其中任何一种都会中断

// Try 1
server.get('/tutorials', (req, res) => {
  fs.createReadStream('./common/content.json').pipe(res);
});

// Try 2
server.get('/tutorials', (req, res) => {
  fs.readFile('./common/content.json', 'utf8', function(err, tutorials) {
    res.send(tutorials);
  });
});

将文件修改为更小的文件(如
{a:1,b:2,c:3}
不会导致我的健全性检查出错。

看起来express在响应中设置了错误的
内容长度
头,因此JSON被截断。您可以显式地设置它,这应该会起作用

server.get('/tutorials', (req, res) => {
    fs.readFile('./common/content.json', 'utf8', function (err, tutorials){
        res.writeHead(200, {
            'Content-Type': 'application/json',
            'Content-Length': Buffer.byteLength(tutorials, 'utf8')
        })
        res.send(tutorials);
    });
});

你能验证你的JSON文件格式是否正确吗?是的,这很好,因为客户端处理它没有问题。很酷,我使用了
res.set
语法,但答案是没有
内容长度