lasselukkari / aWOT

Arduino web server library.
MIT License
283 stars 41 forks source link

static website Rewrite #137

Closed Sampozzo closed 2 years ago

Sampozzo commented 2 years ago

Hello, i'm using your library in my project and i like it very much, thank you for your work! I'm building a project with an angular client flashed with your aWOT-scripts and it works very amazing.

I have a static webserver at root that serve the angular client, and a Json server listening at /api:

app.get("/api", &readAPI);

app.use(staticFiles());

I need to create a rewrite for every request that isn't /api that point to index.html of the static files, like i can do with nginx rewrites:

location / {
    try_files $uri $uri/ /index.html =404;
}

example, i want that http://myip/edit-config rewrite to http://myip/. Can you hep please?

Thank you and sorry for my english!

lasselukkari commented 2 years ago

You can do something like this:

void serveIndex(Request &req, Response &res) {
  if (res.statusSent()) {
    return;
  }

  return static_index(req, res);
}

and then add

app.get(&serveIndex);

as the last middleware after all other routes.

I have not tested this code but I have done it before in the past. Let me know if it works for you.

Another option is to use:

app.notFound(&static_index);

but this way it will not make any difference between GET and other HTTP methods.

Sampozzo commented 2 years ago

Yes! It works! Thank you very much!