Mongoose basic example

216 views
Skip to first unread message

Gustavo Santana

unread,
Apr 25, 2014, 3:23:27 PM4/25/14
to mongoos...@googlegroups.com
Hi everybody!

I compiled the hello.c example but when I acess localhost:8080 I get the message:
"Hello! Requested URI is [/], query string is [(none)]"

I want to load the index.html file that is in the same folder of hello bin

What is the problem?

I'm using ubuntu and here is the source:

#include <stdio.h>
#include <string.h>
#include "mongoose.h"

// This function will be called by mongoose on every new request
static int index_html(struct mg_connection *conn) {
  mg_printf_data(conn, "Hello! Requested URI is [%s], query string is [%s]",
                 conn->uri,
                 conn->query_string == NULL ? "(none)" : conn->query_string);
  return 0;
}

int main(void) {
  struct mg_server *server;
  
  // Create and configure the server
  server = mg_create_server(NULL);
  mg_set_option(server, "listening_port", "8080");
  mg_add_uri_handler(server, "/", index_html);

  // Serve request. Hit Ctrl-C to terminate the program
  printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
  for (;;) {
    mg_poll_server(server, 1000);
  }

  // Cleanup, and free server instance
  mg_destroy_server(&server);

  return 0;
}



Best regards
Gustavo

Sergey Lyubka

unread,
Apr 27, 2014, 9:08:58 AM4/27/14
to mongoose-users
You need to specify document_root option, and return MG_FALSE for the files inside document_root.


--
You received this message because you are subscribed to the Google Groups "mongoose-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-user...@googlegroups.com.
To post to this group, send email to mongoos...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongoose-users.
For more options, visit https://groups.google.com/d/optout.

Nfer Zhuang

unread,
Apr 27, 2014, 9:02:06 PM4/27/14
to mongoos...@googlegroups.com
Hi,
I guess you are working on the version 5.1, the latest has a lot of changes of the APIs.
In this version, if you want to load index.html directly, you need remove "mg_add_uri_handler" fuction on the uri "/", and set the "document_root" by
mg_set_option(server, "document_root", "./");

Nfer

Gustavo Santana

unread,
Apr 30, 2014, 10:32:41 AM4/30/14
to mongoos...@googlegroups.com
Hi Nfer and Sergey,

thanks for the replies ... I'll try them!
Best regards


Atenciosamente,
Gustavo Santana
Diretor de Arte e Tecnologia
www.educatrix.com.br
31 9258 0762 




--
You received this message because you are subscribed to a topic in the Google Groups "mongoose-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongoose-users/Qns4Cn1rMpI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongoose-user...@googlegroups.com.

Sam Kunun

unread,
Jun 18, 2014, 10:52:46 AM6/18/14
to mongoos...@googlegroups.com, gustavo...@gmail.com
Hello,

I try to compile example hello.c to listening port 80 by modifying this line :

mg_set_option(server, "listening_port", "8080");
to
mg_set_option(server, "listening_port", "80");

When I run, I'm not able to load the page.
Did I missed out anything ?

Thank you.
Regards,
~sam

Sergey Lyubka

unread,
Jun 18, 2014, 12:09:43 PM6/18/14
to mongoose-users, gustavo...@gmail.com
Port 80 might be busy, or privileged.


--
You received this message because you are subscribed to the Google Groups "mongoose-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-user...@googlegroups.com.

Terence Martin

unread,
Jun 18, 2014, 12:43:36 PM6/18/14
to mongoos...@googlegroups.com, gustavo...@gmail.com
You should check the return value of mg_set_option(). If it returns NULL, everything is good. Otherwise it will return a string error that explains what went wrong.
Reply all
Reply to author
Forward
0 new messages