Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/dingpingzhang/naivehttpserver
- Owner: DingpingZhang
- License: mit
- Created: 2021-08-28T07:54:40.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-26T08:46:51.000Z (about 2 years ago)
- Last Synced: 2024-10-11T14:29:41.829Z (3 months ago)
- Topics: file-server, http, http-server, simple-http-server
- Language: C#
- Homepage:
- Size: 51.8 KB
- Stars: 6
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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();
```