Development has been moved to here
The code below shows how to initialise an Express server, how to set up a callback to a path and how to send data back to the user.
using ExpressSharp;
...
Express server = new Express();
server.GET("/helloworld", (req, res) =>
{
res.Send("Hello, world!");
});
server.Listen(80);
If you visit your browser at http://localhost/helloworld it should display "Hello, world!", this means that it is working successfully.
TODO
Middleware is code that is executed before your callback is executed, for example I will have a middleware which will just write "Im middleware" to the console when the user accesses a page, however, this can be used for other things such as authentication headers, to avoid repeating code within each callback.
using ExpressSharp;
...
Express server = new Express();
server.Use((req, res, next) =>
{
Console.WriteLine("Im middleware");
next();
});
server.GET("/helloworld", (req, res) =>
{
res.Send("Hello, world!");
});
server.Listen(80);
In this example you may notice the usage of next()
, calling this function calls either the next middleware or the callback depending on whether it is the last middleware to be called, middleware is only called if the path exists as a binding.
- Usable
TatoExp 💻 |