umijs / umi-blog-example

An example of blog website built with Umi.js and new API route feature in Umi 4.
https://umijs.org/docs/tutorials/blog
38 stars 33 forks source link

怎么改请求里的response headers里的contentType为application/json #14

Open JUNHAN12138 opened 11 months ago

JUNHAN12138 commented 11 months ago

我在umi框架里写的api接口怎么改请求里的response headers里的contentType为application/json

`import type { UmiApiRequest, UmiApiResponse } from "umi"; import fs from "fs"; import path from "path";

export default async function (req: UmiApiRequest, res: UmiApiResponse) { switch (req.method) { case "GET": try { const { id } = req.body; const mdxPath = path.join( process.cwd(), "src/mdxFile", ${20231127}.mdx );

    const mdxContent = fs.readFileSync(mdxPath, "utf8");

    res
      .status(200)
      .header("Content-Type", "application/json")
      .json({ content: mdxContent });
  } catch {
    res.status(500).header("Content-Type", "application/json").json({
      result: false,
      message: "报错",
    });
  }
  break;

} }`

请求的接口response一直是这个 image 导致fetch的res.json()报错,