expressglitch-framework

How to create a 404 page without using .htaccess


Do you need help making a 404 page? You can use this with express and with my answer on the bottom.

(if you have a working package.json and you are using express, you can simply just skip this step.)

Add this first to 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"
  ]
}

then look at the answer below


Solution

  • Use this:

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

    Also make sure to make a 404.html page in the views/ folder on glitch. Or, if you want remove the /views/404.html and replace it with /404.html.
    View the demo. Make any random text on the end of the url.