【问题标题】:How to create a 404 page without using .htaccess如何在不使用 .htaccess 的情况下创建 404 页面
【发布时间】:2020-02-23 02:22:30
【问题描述】:

您需要帮助制作 404 页面吗?您可以将其与 express 和底部的我的答案一起使用。

(如果你有一个可用的 package.json 并且你使用的是 express,你可以直接跳过这一步。)

首先将这个添加到 package.json:

{
  "name": "   text   ",
  "version": "0.0.1",
  "description": "   text   ",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.4"
  },
  "engines": {
    "node": "8.x"
  },
  "license": "MIT",
  "keywords": [
    "node",
    "glitch",
    "express"
  ]
}

那就看看下面的答案

【问题讨论】:

    标签: express glitch-framework


    【解决方案1】:

    使用这个:

    app.get('*', function(req, res){
      res.sendFile(__dirname + '/views/404.html');
    });
    

    还要确保在出现故障时在 views/ 文件夹中创建 404.html 页面。或者,如果您想删除 /views/404.html 并将其替换为 /404.html
    View the demo. Make any random text on the end of the url.

    【讨论】:

    • 最好改用app.useGET 以外的方法可以/应该出现 404。
    • 我更喜欢这种方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 2015-02-04
    • 2012-07-20
    • 1970-01-01
    相关资源
    最近更新 更多