Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/dingpingzhang/naivehttpserver

A simple C# http server based on the HttpListener.
https://github.com/dingpingzhang/naivehttpserver

file-server http http-server simple-http-server

Last synced: 2 months ago
JSON representation

A simple C# http server based on the HttpListener.

Awesome Lists containing this project

README

        

# NaiveHttpServer [![version](https://img.shields.io/nuget/v/NaiveHttpServer.svg)](https://www.nuget.org/packages/NaiveHttpServer)

A simple C# http server based on the HttpListener.

## How to Use

```csharp
// Build Routers
var router = new RouterBuilder()
.Get("/user/:id", async ctx =>
{
// Gets parameters from URL or QUERY string by name.
if (ctx.TryGetParameter("id", out string id))
{
// TODO: using the id parameter.
await ctx.Response.Json(new
{
id,
// TODO: other properties.
});
}
})
.Post("/user", async ctx =>
{
dynamic body = ctx.Request.JsonFromBody();
// TODO: using the body object.
})
.Put("/user/:id", async ctx => { /* TODO: Do something. */ })
.Delete("/user/:id", async ctx => { /* TODO: Do something. */ })
.Build();

// Create server instance
var server = new Server("localhost", 2333);

// Configure server
server
.Use(Middlewares.Log)
.Use(Middlewares.Execute)
.Use(router)
.Use(Middlewares.StaticFile("/files", Environment.CurrentDirectory))
.Use(Middlewares.NotFound(documentUrl: "http://api.project.com/v1"));

// Launch server
server.Start();
```