如何在不使用.htaccess的情况下创建404页面

时间:2019-10-27 14:46:38

标签: express glitch-framework

需要帮助制作404页面吗?只能将它与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"
  ]
}

然后查看下面的答案

1 个答案:

答案 0 :(得分:1)

使用此:

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

还请确保在glitch的views /文件夹中创建404.html页面。或者,如果要删除/views/404.html,然后将其替换为/404.html
View the demo. Make any random text on the end of the url.