c++cppcms

cppcms can't work


My OS: Mac OSX
My gcc version: 4.2.1
My clang version: llvm 6.1.0
cppcms version: 1.0.5
I download the cppcms framework and install the framework.The commands: cd cppcms-1.0.5

mkdir build & cd build
cmake ..
make
make test
make install

No error found. The I write a cpp file named hello.cpp. The program is like this:

#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <iostream>

using namespace std;

class hello : public cppcms::application{
public:
    hello(cppcms::service &srv) : cppcms::application(srv){

    }
    virtual void main(std::string url);
};

void hello::main(std::string /*url*/)
{
    response().out() <<
    "<html>\n"
            "<body>\n"
            "  <h1>Hello World</h1>\n"
            "</body>\n"
            "</html>\n";
}

int main(int argc,char ** argv) {
    try {
        cppcms::service srv(argc,argv);
        srv.applications_pool().mount(
                cppcms::applications_factory<hello>()
        );
    }
    catch (std::exception const &e){
        std::cerr << e.what() << std::endl;
    }
    return 0;
}

My config.js:

{
    "service" : {
        "api" : "http",
        "port" : 8008
    },
    "http" : {
        "script_names" : [ "/hello" ]
    }
}

Compile commands:

c++ hello.cpp -lcppcms -o hello
./hello -c config.js 

I visited the url "http://localhost:8008/hello",then web browser show me

"This webpage is not available".

What's wrong? How to fix the problem.


Solution

  • Most importantly you have to start your service after mounting it:

    cppcms::service srv(argc,argv);
        srv.applications_pool().mount(
                cppcms::applications_factory<hello>()
        );
        srv.run();
    

    I would also include the following two header files (at least on Linux I got a compilation error otherwise):

    #include <cppcms/application.h>
    #include <cppcms/applications_pool.h>