【问题标题】:Fetch - get HTML text as a responseFetch - 获取 HTML 文本作为响应
【发布时间】:2022-02-08 00:48:13
【问题描述】:

我有问题。我有返回 HTML 的速率限制:

const Limiter = rateLimit({
  windowMs: 5 * 60 * 1000,
  message: "Too many requests created from this IP, please try again later.",
  max: 10
})

我在客户端有获取该消息的请求:

      const body = { email, password }

      const res = await fetch("http://localhost:5000/api/auth/login", { 
        method: "POST",
        headers: {
          "Content-Type": "application/json"
        },
        body: JSON.stringify(body)
      })

      const parseRes = await res.json()

      if(parseRes.JwtToken) {
        localStorage.setItem("JwtToken", parseRes.JwtToken)

        setAuth(true)
        toast.success("Login successfuly")
      } else {
        setAuth(false)
        toast.error(parseRes)
      } 

我的问题是,当我取回 json 对象时,它会显示在屏幕上。但是当我得到那个message 对象时,它什么也不做。它只是在控制台中显示失败 429。我的问题是如何获取/显示正常返回的 HTML,就像获取 JSON 对象一样。

【问题讨论】:

  • 不应该返回一个不是200的http状态码吗?检查
  • 返回 429(请求过多)

标签: javascript node.js express express-rate-limit


【解决方案1】:

因此,在解析 JSON 之前,请检查 status

if (!res.ok) {
  /* handle the error */
  return;
}
const parseRes = await res.json()

【讨论】:

    猜你喜欢
    • 2017-01-04
    • 1970-01-01
    • 2023-03-18
    • 2017-07-04
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    相关资源
    最近更新 更多