firebase - 如何正确返回 Nuxt 项目中错误页面的 HTTP 状态码 404
问题描述
如果您请求 URL https://www.sistrix.com/I-do-not-exist/,我们没有该页面的内容。由于请求的页面不存在,因此没有内容,我们的网络服务器将在标题中向您显示一个错误页面,它显示 304 作为不正确的状态代码。如何在标题中显示 404 作为状态码。
这是我的代码
<template>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="error-template">
<a href="/">
<img
class="mainlogo"
alt="Company logo"
style="margin-bottom: 11px"
src="https://ik.imagekit.io/your_imagekit_id/rest-of-the-path.jpg"
/>
</a>
<h3 style="color: gray">Oops!</h3>
<h1 v-if="error.statusCode === 401">Page not found</h1>
<h1 v-else>An error occurred- {{ statusCode }}</h1>
<ul>
<li v-for="statusCod in statusCode" :key="statusCod.id" class="item">
<nuxt-link :to="'/statusCode/' + statusCod.id">{{ statusCod.statusCode }}</nuxt-link>
</li>
</ul>
<h1>Hello</h1>
<img alt="company shot" style="width: 20%" src="https://ik.imagekit.io/your_imagekit_id/rest-of-the-path.jpg" />
<div class="error-actions">
<nuxt-link style="font-size: 17px" to="/">Home page</nuxt-link>
</div>
</div>
strong text
</div>
</div>
</div>
</template>
<script>
import axios from '@nuxtjs/axios'
export default {
layout: 'blog',
props: ['error'],
asyncData({ $axios }) {
const { rest } = axios
.post(`http://www.json-generator.com/api/json/get/bTHZjyGrPC?indent=2`)
.then((res) => {
return { statusCode: rest }
})
.catch((e) => {
error({ statusCode: 404, message: 'Post not found' })
})
},
}
</script>
<style scoped>
.error-template {
padding: 14% 15px;
text-align: center;
}
.error-actions {
margin-bottom: 15px;
margin-top: 15px;
}
.error-actions .btn {
margin-right: 10px;
}
.mainlogo {
max-width: 40px;
}
</style>
解决方案
如果您直接进入 404 页面,您应该会得到一些成功的代码,例如200
因为您确实正确地到达了它。
如果你去类似的东西https://www.sistrix.com/nice-fake-url
,你应该有一个 404 错误,然后将用户重定向到error
布局。
还有,什么是http://www.json-generator.com/api/json/get/bTHZjyGrPC?indent=2
?
推荐阅读
- javascript - ArangoDB AQL 片段问题
- gitlab - Devops CICD 在使用 jest + puppeteer 进行测试时运行 ReactJS 应用程序
- java - Spring Boot JPA DataIntegrityViolationException
- c++ - 多线程 TCP 网络的性能
- flutter - 为什么初始路由不是构建 Navigator 后堆栈上的唯一路由?
- javascript - 数组中的重叠图像 - Javascript
- node.js - Node.js 将 HTML 作为响应发送给客户端
- javascript - 味精:javascript 错误:无法在“文档”上执行“elementsFromPoint”:提供的双精度值是非有限的
- python - 将 hddd° mm.mm' 转换为十进制度
- excel - 当我用我的 VB 代码编辑 XML 文件时,原始结构消失了,所有节点都写在一行中