Skip to content

DingpingZhang/NaiveHttpServer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NaiveHttpServer version

A simple C# http server based on the HttpListener.

How to Use

// 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<User>();
        // 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();

About

A simple C# http server based on the HttpListener.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages