Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 404 page #30

Closed
sthomp opened this issue May 25, 2013 · 6 comments
Closed

Add 404 page #30

sthomp opened this issue May 25, 2013 · 6 comments

Comments

@sthomp
Copy link

sthomp commented May 25, 2013

Right now if I specify a path that doesn't exist I get a page that says:

Cannot GET /some/path

Would be nice to specify a 404.html file instead. Or, in the case of angularjs apps would like to specify index.html to handle routing.

@iObject
Copy link

iObject commented Jun 13, 2013

I would LOVE this!

@shama
Copy link
Member

shama commented Sep 2, 2013

I think custom 404 handling should happen in your own middleware instead. Thanks for the suggestion though!

@shama shama closed this as completed Sep 2, 2013
@Siyfion
Copy link

Siyfion commented Oct 23, 2013

@shama Say you wanted to handle all 404s by returning the index.html page (which is quite often the case in a single page app), how would I go about doing this? Do I have access to the fs module?

I thought something like the below config might have worked, then I realised that it's only connect, not the full express framework.

      livereload: {
        options: {
          open: true,
          base: [
            '.tmp',
            '<%= yeoman.app %>'
          ],
          middleware: function(connect, options) {
            var middlewares = [];
            var directory = options.directory || options.base[options.base.length - 1];
            if (!Array.isArray(options.base)) {
              options.base = [options.base];
            }
            options.base.forEach(function(base) {
              // Serve static files.
              middlewares.push(connect.static(base));
            });
            // Make directory browse-able.
            middlewares.push(connect.directory(directory));
            // Handle 404
            middlewares.push(function(req, res, next) {
              res.sendfile(options.base[1] + '/index.html');
            });
            return middlewares;
          }
        }
      },

@shama
Copy link
Member

shama commented Oct 23, 2013

@Siyfion You could build your own if you need something simple:

middleware: function(connect, options) {
  return [function(req, res) {
    var filename = '.' + req.url;
    if (!grunt.file.exists(filename)) filename = './404.html';
    res.end(grunt.file.read(filename));
  }];
},

But by that point it would probably be easier to use one of the many file serving libraries intended for serving files in a more production like environment (express, st, estatic, etc) or something like https://npmjs.org/package/grunt-express

@szalishchuk
Copy link

I had the same issue myself, after couple of hours of research, here is the solution I've ended up with.

@stevenvachon
Copy link

Here's what helped me: https://gist.github.com/ssafejava/8704372

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants