Creating Pages & Routes

To create pages in NodeC++ you need to add routes that route your request to the right page. All of the routes require a callback function (this function will get called every single time a request is made to the path). I’m going to create a very simple index page.

Creating the callback function

I’m going to name my callback function indexCallback.

1
2
3
4
5
void indexCallback(Request req, responce res) {
    res.write("Index Page");
    res.send("200");
    res.end();
}

While most of what this function does will be covered in the responceClass, i’ll just go over the very important bits. The first line (where the function is decleared), requires two paramaters:

  • a Request Struct
  • a responce class

Warning

Without the correct paramaters the program will (most likley) not compile correctly

Registering the callback function

Currently, we have a callback function, but it does nothing. It is never called.

In order for us to be able to view the page, we need to registar it with the http_server object.

So back where the http_server object was defined, we need to add the following:

httpServer.path_callback("/", &indexCallback);

Note

You need to add the callback before you start the server