import { getSortedSitesData } from './Site';

export default async function handler(req, res) {
  const allPostsData = await getSortedSitesData();
  const formattedPosts = allPostsData.map(post => {
    return {
      ...post,
      date: post.date.toString()
    };
  });
  const totalPosts = formattedPosts.length;

  res.status(200).json({ totalPosts });
}

// 注意:这个 API 需要在 pages/api/total.js 文件中创建,并且需要在 Next.js 应用程序中进行正确的路由配置。

该 API 使用 getSortedSitesData 函数获取所有网站数据,然后对数据进行格式化,并统计网站总数,最后以 JSON 格式返回结果。

在 Next.js 应用程序中,可以通过以下方式访问该 API:

const response = await fetch('/api/total');
const data = await response.json();
console.log(data.totalPosts);

这将返回一个包含 totalPosts 属性的对象,其中包含网站总数。

Next.js API: 获取网站总数

原文地址: https://www.cveoy.top/t/topic/mYWV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录