小编Phi*_*hil的帖子

在 Nextjs 中未发送响应的 API 已解析

我必须在我的 nextjs 项目中进行相同的更改,因为我的 enpoint API 不支持很多调用,我想每 3 分钟刷新一次原始数据。

我从 nextjs 实现了 API:我创建了一个pages/api/data并在内部调用了我的端点,并在我的getInitialProps内部索引调用中调用了数据文件。

get 工作正常,但我有两个问题:

1:我有警告消息说:

API 在不发送 /api/data 响应的情况下解析,这可能会导致请求停止。

2:它在 3 分钟后没有重新加载数据......我认为这是因为缓存控制值......

这是我的代码:

页面/API/数据

import { getData } from "../../helper";

export default async function(req, res) {
  getData()
    .then(response => {
      res.statusCode = 200
      res.setHeader('Content-Type', 'application/json');
      res.setHeader('Cache-Control', 'max-age=180000');
      res.end(JSON.stringify(response))
    })
    .catch(error => {
      res.json(error);
      next();
    });
};
Run Code Online (Sandbox Code Playgroud)

页/索引

import React, { useState, useEffect } from "react";
import fetch from 'isomorphic-unfetch'

const Index = props => …
Run Code Online (Sandbox Code Playgroud)

javascript rest node.js reactjs next.js

13
推荐指数
4
解决办法
2万
查看次数

标签 统计

javascript ×1

next.js ×1

node.js ×1

reactjs ×1

rest ×1