Git Product home page Git Product logo

express's Introduction

express server - beta

Simple REST Framework

###Getting Started

A simple project here

Requires

Assuming you've already installed express framework, create a WebBroker - Web Server Application to your project (File -> New -> Other -> WebBroker -> Web Server Application).

The following code must be inserted in the "WebModule.pas" which is automatically generated in the "DefaultHandlerAction" event.

uses Express;
...
procedure TWebModule1.WebModule1DefaultHandlerAction(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
begin
  App.Call(Request, Response);
end;

###Samples

unit Test;

interface

uses Express;

type
  TContent = record // or class
    Name: String;
    SurName: String;
  end;
  
  TResult = record // or class
    Success: Boolean;
  end;
  
  [Location('api/test')] // this optional
  TTest = class(TProvider)
  public
    [GET] [DEFAULT] // default attribute is optional. 
    function Multiply(A, B: Integer): Integer;  

    [GET]
    function Divide(A, B: Integer): Integer; 
    
    [DELETE] [DEFAULT] // default attribute is optional.
    function Del(Id: Integer): TResult;
    
    [POST] [DEFAULT] // default attribute is optional.
    function Insert(Id: Integer; Content: TContent): TResult; // Content is optional
    
    [PUT] [DEFAULT] // default attribute is optional.
    function Update(Id: Integer; Content: TContent): TResult; // Content is Optional
  end;
  
****

initialization
  TClassManager.Register(TTest);  

OR

unit Test;

interface

uses SysUtils, Express;

initialization

App.Get('api/test/multiply/:a/:b', procedure(Req: TRequest; Res: TResponse)
begin
  Res.Send(Req.Params['a'].toInteger * Req.Params['b'].ToInteger);
end));

// ***
// * The default attribute is like this route
// ***
App.Get('api/test/:a/:b', procedure(Req: TRequest; Res: TResponse)
begin
  Res.Send(Req.Params['a'].toInteger * Req.Params['b'].ToInteger);
end));

App.Del('api/test/del/:id', procedure(Req: TRequest; Res: TResponse) 
var
  Result: TResult;
begin
  Result.Success := Req.Params['id'].toInteger = 1;
  Res.SendRecord<TResult>(Result);
end));

App.Post('api/test/insert/:id', procedure(Req: TRequest; Res: TResponse) 
var
  Content: TContent;
  Result: TResult;
begin
  Content := TSuperRecord<TContent>.FromJSON(Req.WebRequest.Content);
  Result := Content.Name = 'Onur';
  Res.SendRecord<TResult>(Result);
end));

App.Put('api/test/update/:id', procedure(Req: TRequest; Res: TResponse) 
var
  Content: TContent;
  Result: TResult;
begin
  Content := TSuperRecord<TContent>.FromJSON(Req.WebRequest.Content);
  Result := Content.Name = 'YILDIZ';
  Res.SendRecord<TResult>(Result);
end));

Aspec Oriented Programming

unit AOPTest;

interface

uses Express;

type

  [Location('api/test')]
  TTest = class(TInject)
  public
    [GET]
    function Multiply(A, B: Integer): Integer;
  end;
  
****

initialization
  TClassManager.Register(TTest);  

OR

unit AOPTest;

interface

uses SysUtils, Express;

initialization

App.Use(hmGet, 'api', procedure(Req: TRequest; Res: TResponse; var Next: Boolean)
begin
  Next := False;
  Res.Send('Unauthorized (route)');
end));

App.Use(hmGet, 'api/test/multiply', procedure(Req: TRequest; Res: TResponse; var Next: Boolean)
begin
  Next := False;
  Res.Send('Unauthorized (method)');
end));

App.Use(hmGet, 'api/test/multiply/:a/:b', procedure(Req: TRequest; Res: TResponse; var Next: Boolean)
begin
  Next := Req.Params['a'].toInteger = 1;
  Res.Send('Unauthorized (method paremeter)');
end));

express's People

Contributors

onryldz avatar

Watchers

 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.