Hello World
In the code snippet below you can see the basics of the WebBeaver framework in the form of an Hello World project.
This program starts a http server and listens on port 80 for connections. The program responds with "Hello World" when a request is send with the root URL (/). This URL is also named route.
Installing with command line
You can install WebBeaver with the following command provided that you have installed dotnet.
dotnet add package WebBeaver
Note
Make sure your command line working directory is the same as your project directory.
Installing with Visual Studio
Follow the following steps to install the WebBeaver library with Visual Studio for your project:
- Load a project in Solution Explorer, and then select Project > Manage NuGet Packages.
- Select the Browse tab, select the search box, and then type WebBeaver into the search box.
- Click on the WebBeaver package and click the install button.
Creating an Http server
In the following example a http object is created and given port 80 to listen on. After that the server is started using the Start()
method.
Http http = new Http(80);
http.Start();
Warning
The Start()
method blocks the thread it is executed in.
Creating an Https server
Creating an Https server is simular to creating an Http server, but for an Https server a certificate is needed.
So before we continue make sure you have a certificate placed in your project directory. A tutorial for generating a self-signed certificate can be found here.
In the following example a https object is created and given port 443 to listen on, and a certificate is attached to it. After that the server is started using the Start()
method.
Https server = new Https(443);
server.Certificate(
new X509Certificate2(
// The certificate file location
Http.RootDirectory + "/certificate_name_here.pfx",
// The certificate password
"certificate_password_here"
)
);
server.Start()
Warning
The Start()
method blocks the thread it is executed in.
Creating a Router
When using the WebBeaver routing we create a Router object. This object will hold all our endpoints and works together with an Http(s) server to route the client requests to the correct endpoint.
In the following example we create a new Router object and give a Http server to it.
Router router = new Router(server);
Note
Make sure the router is fully setup (endpoints added, static folder set, etc.) before starting the Http server.
Route static files:
When using a Router object a folder can be set where all static files (js, css, images, ...) can be placed.
In the following example the public folder within the project directory is set as the Static folder.
router.Static("public");
After this function is called files and folders that are put in the public folder are available with URIs. An example would be /css/style.css
Creating endpoints
An endpoint in WebBeaver is defined as a method with a Route attribute and the (Request req, Response res)
arguments.
Basic Endpoint:
In the following example we create a basic endpoint with the name Index and the path /.
So this endpoint will respond to any client requests to root url / with Hello World
.
[Route("/")]
void Index(Request req, Response res)
{
res.Send("Hello World");
}
Http method Endpoint:
In the following example we create a endpoint with the name IndexPost, the path / and the HTTP request method POST
.
This endpoint works almost the same as the basic endpoint but the client needs to use the HTTP POST
request method when sending its request.
A list of all Http methods can be found here.
[Route("POST", "/")]
void IndexPost(Request req, Response res)
{
res.Send("Hello World");
}
Endpoint Collection:
In the following example we create an class that will contain the endpoints for examples we want to show.
To make each endpoint route start with /examples we add a Route attribute to our class (this is not mandatory).
[Route("/examples")]
class ExampleContoller
{
[Route("/")]
static void Index(Request req, Response res)
{
res.SendFile("example_list.html");
}
[Route("/dotnet")]
static void DotnetExamples(Request req, Response res)
{
res.SendFile("examples_dotnet.html");
}
[Route("/js")]
static void JavaScriptExamples(Request req, Response res)
{
res.SendFile("examples_js.html");
}
}
Importing endpoints
When we have created some endpoints they won't do anything yet, we first have to add them to the router.
This is done with the Import()
method.
In the following example we add the endpoint Index to our router.
router.Import(Index);
After adding the endpoint can be called when a client request matches it's route attribute values.
Importing Endpoint Collections:
In the following example we will import a collection (class) of endpoints.
router.Import<EndpointCollectionClass>();
This will import all static methods in the class as endpoints.