Skip to content

How to to server index.html from public folder if 404 occurs. #7757

Answered by rajkaran
rajkaran asked this question in Q&A
Discussion options

You must be logged in to vote

Finally I figured it out with small adjustment to my original code. I pasting my solution here for someone having same issue in future.

async value() {
    const middleware: Middleware = async (ctx: MiddlewareContext, next: Next) => {
      try {
        return await next();
      } catch (error) {
        if (error.statusCode === 404) {
          ctx.response.sendFile('public/index.html', {root: './'})
        } else {
          throw error;
        }

        return;
      }
    };
    return middleware;
  }

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@rajkaran
Comment options

@mrmodise
Comment options

@rajkaran
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by rajkaran
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants