Giter Site home page Giter Site logo

bubdm / sharpexpress Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sergeyt/sharpexpress

0.0 0.0 0.0 631 KB

Functional API to process HTTP requests with ASP.NET MVC-like routing inspired by express.js

License: MIT License

Shell 0.21% C# 99.18% Makefile 0.24% Batchfile 0.38%

sharpexpress's Introduction

Build Status Build status NuGet Version NuGet Downloads

SharpExpress

Functional API to process HTTP requests with ASP.NET MVC-like routing inspired by express.js

Features

  • Lightweight library - now ~164kb
  • Extensible
  • Built-in light http server
  • Based on .NET BCL only without external dependencies
  • Using System.Web.Routing for url routing
  • Compilable and runnable on Mono
  • Runnable in ASP.NET applications since ExpressApplication implements IHttpHandler
  • Use WebService extension to handle requests using existing web service class
  • Use HttpHandler extension to handle requests using existing IHttpHandler implementations
  • Use Static extension to serve static files
  • Use Embedded extension to serve emmbedded resources of application assemblies

Examples

Simple Static Server

var app = new ExpressApplication();
app.Static("", Environment.CurrentDirectory);

var settings = new HttpServerSettings { Port = 81, WorkerCount = 4 };
using (new HttpServer(app, settings))
{
	Console.WriteLine("Listening port {0}. Press enter to stop the server.", port);
	Console.ReadLine();
}

Routes API

var app = new ExpressApplication();
app.Get(
	"",
	req => req.Text("Hi!")
	);

app.Get(
	"text/{x}/{y}",
	req => req.Text(
		string.Format("x={0}, y={1}",
			req.RouteData.Values["x"],
			req.RouteData.Values["y"])
		));

app.Get(
	"json/{x}/{y}",
	req => req.Json(
		new
		{
			x = req.RouteData.Values["x"],
			y = req.RouteData.Values["y"]
		}));

var settings = new HttpServerSettings { Port = 81, WorkerCount = 4 };
using (new HttpServer(app, settings))
{
	Console.WriteLine("Listening port {0}. Press enter to stop the server.", port);
	Console.ReadLine();
}

Functional API

Func<double, double> square = x => x * x;

var app = new ExpressApplication();
app.Json("math/json/square/{x}", square)
   .Xml("math/xml/square/{x}", square);

var settings = new HttpServerSettings { Port = 81, WorkerCount = 4 };
using (new HttpServer(app, settings))
{
	Console.WriteLine("Listening port {0}. Press enter to stop the server.", port);
	Console.ReadLine();
}

WebService Extension

internal class MathService
{
	[WebMethod]
	public double Square(double x)
	{
		return x * x;
	}

	[WebMethod]
	public double Add(double x, double y)
	{
		return x + y;
	}
}

var app = new ExpressApplication();
app.WebService<MathService>("math.svc");

var settings = new HttpServerSettings { Port = 81, WorkerCount = 4 };
using (new HttpServer(app, settings))
{
	Console.WriteLine("Listening port {0}. Press enter to stop the server.", port);
	Console.ReadLine();
}

sharpexpress's People

Contributors

sergeyt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.