Page 1 of 1

ESP32 Mongoose webserver serving static files

Posted: Mon Feb 13, 2017 9:02 pm
by Maq_aux
Dear community,

my name is Markus and I´m quite new to the ESP32 and its programming.
At the moment I try to expand the Mongoose webserver example from https://github.com/nkolban/esp32-snippe ... /webserver to be able to serve other static files than my "index.html" but I dont get it working. In the example below i added a converted "jquery_1_11_3_min_js.h" file which was converted with xxd according to the instructions on the linked git repo.

I tried to expand the Mongoose event handler as shown below but my browser (tried it with Firefox, Chrome and IE) doesnt recognize the transmitted data as Javascript (something wrong with the header?).

Code: Select all

// Mongoose event handler.
void mongoose_event_handler(struct mg_connection *nc, int ev, void *evData) {
    switch (ev) {
    case MG_EV_HTTP_REQUEST: {
            struct http_message *message = (struct http_message *) evData;
            char *uri = mgStrToStr(message->uri);
            printf(uri);
            printf("\n");
            if (strcmp(uri, "/time") == 0) {
                char payload[256];
                struct timeval tv;
                gettimeofday(&tv, NULL);
                sprintf(payload, "Time since start: %d.%d secs", (int)tv.tv_sec, (int)tv.tv_usec);
                int length = strlen(payload);
                mg_send_head(nc, 200, length, "Content-Type: text/plain");
                mg_printf(nc, "%s", payload);
            } else if (strcmp(uri, "/test1.html") == 0) {
                mg_send_head(nc, 200, test1_html_len, "Content-Type: text/html");
                mg_send(nc, test1_html, test1_html_len);
            } else if (strcmp(uri, "/index.html") == 0) {
                printf("INDEX requested \n");
                mg_send_head(nc, 200, index_html_len, "Content-Type: text/html");
                mg_send(nc, index_html, index_html_len);
            } else if (strcmp(uri, "/jquery-1.11.3.min.js") == 0) {
                printf("JS file requested \n");
                mg_send_head(nc, 200, jquery_1_11_3_min_js_len, "Content-Type: application/javascript");
                mg_send(nc, jquery_1_11_3_min_js, jquery_1_11_3_min_js_len);
            }    else {
                mg_send_head(nc, 404, 0, "Content-Type: text/plain");
            }
            nc->flags |= MG_F_SEND_AND_CLOSE;
            free(uri);
            break;
        }
    } // End of switch
} // End of mongoose_event_handler

Many thanks in advance and kind regards from germany!
Markus