lasselukkari / aWOT

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

routing use .use appends 404 error after content is served #11

Closed shanevanj closed 6 years ago

shanevanj commented 6 years ago

Using hello world example:

Output in browser if app.get("", &indexCmd); is used to route

Greetings middle earth!

Output in browser if app.use(&indexCmd); is used to route

Greetings middle earth!

HTTP/1.0 404 Not Found Content-Type: text/html

404 Not Found

I want to deal with the routing myself as I will serve files from SD card as well as using Ajax

lasselukkari commented 6 years ago

Update to the latest version and call res.end() before exiting your handler. After res.end() is called no more handlers are executed. This will also prevent the Not Found page from appearing.

#include <WiFi.h>
#include "aWOT.h"

WiFiServer server(80);
WebApp app;

void indexCmd(Request &req, Response &res) {
  P(index) =
    "<html>\n"
    "<head>\n"
    "<title>Hello World!</title>\n"
    "</head>\n"
    "<body>\n"
    "<h1>Greetings middle earth!</h1>\n"
    "</body>\n"
    "</html>";

  res.success("text/html");
  res.printP(index);
  res.end();
}

void setup() {
  Serial.begin(115200);

  WiFi.begin();
  long abortConnect = millis() + 100000;
  while (WiFi.status() != WL_CONNECTED && millis() < abortConnect) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  server.begin();

  app.use(&indexCmd);
}

void loop() {
  WiFiClient client = server.available();

  if (client.connected()) {
    app.process(&client);
  }
}
shanevanj commented 6 years ago

Thanks so much - I would have given it a go, but my skills are just not put to your code - great library !

Shane van Jaarsveldt Tel: +27 11 469 4750 Cel: +27 82 601 0499 Fax: +27 11 469 3370

On 21 Feb 2018, at 21:55, Lasse Lukkari notifications@github.com wrote:

Closed #11 https://github.com/lasselukkari/aWOT/issues/11 via #12 https://github.com/lasselukkari/aWOT/pull/12.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/lasselukkari/aWOT/issues/11#event-1485496373, or mute the thread https://github.com/notifications/unsubscribe-auth/AKhBu8OpxAQagrrsbo8V-oUBG76FRZBXks5tXHS-gaJpZM4SIbxK.