Red
vements
Kerangka kerja aplikasi web Standar .NET yang dibangun di atas ASP.NET Core dengan Kestrel dan terinspirasi oleh kesederhanaan Express.js
RedHttpServer dapat diinstal dari NuGet: Install-Package RHttpServer
RedHttpServer dibuat agar mudah dibangun. Server mendukung modul middleware dan modul ekstensi
Lebih banyak ekstensi dan middleware
var server = new RedHttpServer ( 5000 , "public" ) ;
server . Use ( new EcsRenderer ( ) ) ;
server . Use ( new CookieSessions < MySession > ( TimeSpan . FromDays ( 1 ) ) ) ;
// Authentication middleware
async Task < HandlerType > Auth ( Request req , Response res )
{
if ( req . GetData < MySession > ( ) != null )
{
return HandlerType . Continue ;
}
await res . SendStatus ( HttpStatusCode . Unauthorized ) ;
return HandlerType . Final ;
}
var startTime = DateTime . UtcNow ;
// Url parameters
server . Get ( "profile/:username" , Auth , ( req , res ) =>
{
var username = req . Context . ExtractUrlParameter ( "username" ) ;
// ... lookup username in database or similar and fetch profile ...
var user = new { FirstName = "John" , LastName = "Doe" , Username = username } ;
return res . SendJson ( user ) ;
} ) ;
// Using forms
server . Post ( "/login" , async ( req , res ) =>
{
var form = await req . GetFormDataAsync ( ) ;
// ... some validation and authentication ...
await res . OpenSession ( new MySession { Username = form [ "username" ] } ) ;
return await res . SendStatus ( HttpStatusCode . OK ) ;
} ) ;
server . Post ( "/logout" , Auth , async ( req , res ) =>
{
var session = req . GetData < MySession > ( ) ;
await res . CloseSession ( session ) ;
return await res . SendStatus ( HttpStatusCode . OK ) ;
} ) ;
// Simple redirects
server . Get ( "/redirect" , Auth , ( req , res ) => res . Redirect ( "/redirect/test/here" ) ) ;
// File uploads
Directory . CreateDirectory ( "uploads" ) ;
server . Post ( "/upload" , async ( req , res ) =>
{
if ( await req . SaveFiles ( "uploads" ) )
return await res . SendString ( "OK" ) ;
else
return await res . SendString ( "Error" , status : HttpStatusCode . NotAcceptable ) ;
} ) ;
server . Get ( "/file" , ( req , res ) => res . SendFile ( "somedirectory/animage.jpg" ) ) ;
// Using url queries
server . Get ( "/search" , Auth , ( req , res ) =>
{
string searchQuery = req . Queries [ "query" ] ;
string format = req . Queries [ "format" ] ;
// ... perform search using searchQuery and return results ...
var results = new [ ] { "Apple" , "Pear" } ;
if ( format == "xml" )
return res . SendXml ( results ) ;
else
return res . SendJson ( results ) ;
} ) ;
// Markdown rendering
server . Get ( "/markdown" , ( req , res ) => res . RenderFile ( "markdown.md" ) ) ;
// Esc rendering
server . Get ( "/serverstatus" , async ( req , res ) => await res . RenderPage ( "pages/statuspage.ecs" ,
new RenderParams
{
{ "uptime" , ( int ) DateTime . UtcNow . Subtract ( startTime ) . TotalSeconds } ,
{ "version" , Red . RedHttpServer . Version }
} ) ) ;
// Using websockets
server . WebSocket ( "/echo" , async ( req , wsd ) =>
{
wsd . SendText ( "Welcome to the echo test server" ) ;
wsd . OnTextReceived += ( sender , eventArgs ) => { wsd . SendText ( "you sent: " + eventArgs . Text ) ; } ;
return HandlerType . Final ;
} ) ;
// Keep the program running easily (async Program.Main - C# 7.1+)
await server . RunAsync ( ) ;
Karena saya menyukai C# dan .NET Core, tetapi sering kali membutuhkan server web yang sederhana namun kuat untuk beberapa proyek. Express.js ringkas dan mudah digunakan, sehingga API terinspirasi oleh hal itu.
RedHttpServer dirilis di bawah lisensi MIT, sehingga gratis untuk digunakan, bahkan dalam proyek komersial.