RHttpServer 2.0.5
See the version list below for details.
dotnet add package RHttpServer --version 2.0.5
NuGet\Install-Package RHttpServer -Version 2.0.5
<PackageReference Include="RHttpServer" Version="2.0.5" />
paket add RHttpServer --version 2.0.5
#r "nuget: RHttpServer, 2.0.5"
// Install RHttpServer as a Cake Addin #addin nuget:?package=RHttpServer&version=2.0.5 // Install RHttpServer as a Cake Tool #tool nuget:?package=RHttpServer&version=2.0.5
RedHttpServer
Cross-platform http server with websocket support
A C# alternative to nodejs and similar server bundles
Some of the use patterns has been inspired by nodejs and expressjs
Documentation
Documentation for .NET Core version
Documentation for .NET Framework version
Installation
RedHttpServer can be installed from NuGet: Install-Package RHttpServer
Examples
// We serve static files, such as index.html from the 'public' directory
var server = new RedHttpServer(5000, "public");
var startTime = DateTime.UtcNow;
// We log to terminal here
var logger = new TerminalLogging();
server.Plugins.Register<ILogging, TerminalLogging>(logger);
// URL param demo
server.Get("/:param1/:paramtwo/:somethingthird", async (req, res) =>
{
await res.SendString($"URL: {req.Params["param1"]} / {req.Params["paramtwo"]} / {req.Params["somethingthird"]}");
});
// Redirect to page on same host
server.Get("/redirect", async (req, res) =>
{
await res.Redirect("/redirect/test/here");
});
server.Post("/register", async (req, res) =>
{
var registerForm = await req.GetFormDataAsync();
CreateUser(registerForm["username"][0], registerForm["password"][0]);
SaveUserImage(registerForm["username"][0], registerForm.Files[0]);
await res.SendString("User created!");
});
// Save uploaded file from request body
Directory.CreateDirectory("./uploads");
server.Post("/upload", async (req, res) =>
{
if (await req.SaveBodyToFile("./uploads"))
{
await res.SendString("OK");
// We can use logger reference directly
logger.Log("UPL", "File uploaded");
}
else
await res.SendString("Error", status: 413);
});
server.Get("/file", async (req, res) =>
{
await res.SendFile("testimg.jpeg");
});
// Using url queries to generate an answer
server.Get("/hello", async (req, res) =>
{
var queries = req.Queries;
var firstname = queries["firstname"][0];
var lastname = queries["lastname"][0];
await res.SendString($"Hello {firstname} {lastname}, have a nice day");
});
// Rendering a page for dynamic content
server.Get("/serverstatus", async (req, res) =>
{
await res.RenderPage("./pages/statuspage.ecs", new RenderParams
{
{ "uptime", DateTime.UtcNow.Subtract(startTime).TotalHours },
{ "versiom", RedHttpServer.Version }
});
});
// WebSocket echo server
server.WebSocket("/echo", (req, wsd) =>
{
// We can also use the logger from the plugin collection
wsd.ServerPlugins.Use<ILogging>().Log("WS", "Echo server visited");
wsd.SendText("Welcome to the echo test server");
wsd.OnTextReceived += (sender, eventArgs) =>
{
wsd.SendText("you sent: " + eventArgs.Text);
};
});
server.Start();
Static files
When serving static files, it not required to add a route action for every static file. If no route action is provided for the requested route, a lookup will be performed, determining whether the route matches a file in the public file directory specified when creating an instance of the RedHttpServer class.
Plug-ins
RedHttpServer is created to be easy to build on top of. The server supports plug-ins, and offer a method to easily add new functionality. The plugin system works by registering plug-ins before starting the server, so all plug-ins are ready when serving requests. Some of the default functionality is implemented through plug-ins, and can easily customized or changed entirely. The server comes with default handlers for json and xml (ServiceStack.Text), page renderering (ecs). You can easily replace the default plugins with your own, just implement the interface of the default plugin you want to replace, and register it before initializing default plugins and/or starting the server.
The .ecs file format
The .ecs file format is merely an extension used for html pages with ecs-tags.
Tags
<% foo %> will get replaced with the text data in the RenderParams object passed to the renderer
<%= foo =%> will get replaced with a HTML encoded version of the text data in the RenderParams object passed to the renderer
<¤ files/style.css ¤> will get replaced with the content of the file with the specified path. Must be absolute or relative to the server executable. Only html, ecs, js, css and txt is supported for now, but if you have a good reason to include another filetype, please create an issue regarding that.
The file extension is enforced by the default page renderer to avoid confusion with regular html files without tags.
The format is inspired by the ejs format, though you cannot embed JavaScript or C# for that matter, in the pages.
Embed your dynamic content using RenderParams instead of embedding the code for generation of the content in the html.
Why?
Because i like C#, the .NET framework and type-safety, but i also like the use-patterns of nodejs, with expressjs especially.
License
RedHttpServer is released under MIT license, so it is free to use, even in commercial projects.
Buy me a beer?
16B6bzSgvBBprQteahougoDpbRHf8PnHvD (BTC)
0x63761494aAf03141bDea42Fb1e519De0c01CcF10 (ETH)
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 is compatible. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Framework | net452 is compatible. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETFramework 4.5.2
- HttpMultipartParser (>= 2.2.4)
- ServiceStack.Text (>= 4.5.14)
-
.NETStandard 2.0
- HttpMultipartParser (>= 2.2.4)
- Microsoft.AspNetCore.Cors (>= 2.0.0)
- Microsoft.AspNetCore.Routing (>= 2.0.0)
- Microsoft.AspNetCore.Server.Kestrel (>= 2.0.0)
- Microsoft.AspNetCore.StaticFiles (>= 2.0.0)
- Microsoft.AspNetCore.WebSockets (>= 2.0.0)
- ServiceStack.Text.Core (>= 1.0.44)
NuGet packages (7)
Showing the top 5 NuGet packages that depend on RHttpServer:
Package | Downloads |
---|---|
Red.CookieSessions
Simple session management middleware for RedHttpServer. Uses cookies with authentication tokens |
|
Red.JwtSessions
Package Description |
|
Butterfly.Web.RedHttpServer
Implementation of Butterfly.Web for RedHttpServer (see https://github.com/rosenbjerg/Red) |
|
Red.HandlebarsRenderer
Extension for Red for rendering Handlebars templates as responses |
|
Red.EcsRenderer
Extension for Red for rendering .ecs files |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
5.2.1 | 10,742 | 3/16/2020 | |
5.2.0 | 1,498 | 3/1/2020 | |
5.1.0 | 2,971 | 2/22/2020 | |
5.0.0 | 1,444 | 2/21/2020 | |
4.0.2 | 13,080 | 9/23/2019 | |
4.0.1 | 1,430 | 9/16/2019 | |
4.0.0 | 4,627 | 4/29/2019 | |
3.5.2 | 5,781 | 9/12/2018 | |
3.5.1 | 1,626 | 9/7/2018 | |
3.5.0 | 1,795 | 8/17/2018 | |
3.4.0 | 1,705 | 8/15/2018 | |
3.3.0 | 1,764 | 8/13/2018 | |
3.2.2 | 1,809 | 8/6/2018 | |
3.2.1 | 2,000 | 6/27/2018 | |
3.2.0 | 2,627 | 6/26/2018 | |
3.1.0 | 2,812 | 5/20/2018 | |
3.0.0 | 5,664 | 3/26/2018 | |
2.0.5 | 2,803 | 10/16/2017 | |
2.0.4 | 1,892 | 10/16/2017 | |
2.0.3 | 2,422 | 5/13/2017 | |
2.0.2 | 2,463 | 4/27/2017 | |
2.0.1 | 2,471 | 4/23/2017 | |
2.0.0 | 2,505 | 4/12/2017 | |
1.0.4.4 | 1,922 | 1/25/2017 | |
1.0.4.3 | 1,947 | 1/24/2017 | |
1.0.4.2 | 1,939 | 1/9/2017 | |
1.0.4.1 | 2,199 | 12/12/2016 | |
1.0.4 | 2,061 | 12/12/2016 | |
1.0.3.9 | 1,913 | 12/9/2016 | |
1.0.3.8 | 1,859 | 12/3/2016 | |
1.0.3.7 | 2,241 | 11/8/2016 | |
1.0.3.6 | 1,935 | 10/25/2016 | |
1.0.3.5 | 1,855 | 10/13/2016 | |
1.0.3.4 | 2,043 | 10/5/2016 | |
1.0.3.3 | 1,949 | 9/27/2016 | |
1.0.3.2 | 1,985 | 9/25/2016 | |
1.0.3.1 | 2,061 | 9/22/2016 | |
1.0.3 | 2,084 | 9/22/2016 | |
1.0.2.11 | 2,196 | 9/17/2016 | |
1.0.2.10 | 1,959 | 9/14/2016 | |
1.0.2.9 | 1,878 | 9/14/2016 | |
1.0.2.8 | 2,231 | 9/13/2016 | |
1.0.2.7 | 1,850 | 9/12/2016 | |
1.0.2.6 | 1,884 | 9/12/2016 | |
1.0.2.5 | 1,892 | 9/12/2016 | |
1.0.2.4 | 2,182 | 9/11/2016 | |
1.0.2.3 | 1,846 | 9/11/2016 |
Fixed ASPNETCore version target framework