Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Web APIs with Node.js for .NET developers

Web APIs with Node.js for .NET developers

JavaScript - for a lot of developers it's the typical jQuery syntax "$(...)" creating spaghetti code. And then use JavaScript for some serious backend development? Node.js changed that a lot. The client-only view of JavaScript has ended. Node.js can be used to develop modern and lightweight web APIs using runtime advantages like asynchronous I/O and performance, without having to quit token authentication or database access.

GitHub: https://github.com/thinktecture/nodejs-aspnetcore-webapi/

Manuel Rauber

February 18, 2016
Tweet

More Decks by Manuel Rauber

Other Decks in Programming

Transcript

  1. WEB APIS
    WITH JAVASCRIPT & NODE.JS
    FOR .NET DEVELOPERS
    Manuel Rauber
    thinktecture.com

    View full-size slide

  2. INTRODUCTION
    Manuel Rauber
    [email protected]
    @manuelrauber
    https://manuel-rauber.com

    View full-size slide

  3. TALKING POINTS
    Web APIs
    Node.js
    Restify
    Token
    Authentication
    Databases
    Examples

    View full-size slide

  4. WEB APIS - OVERVIEW

    View full-size slide

  5. WEB APIS - OVERVIEW
    REST APIs, HTTP APIs, whatever APIs
    Model functional services with dedicated interfaces
    Can use several other services for data aggregation
    Secured by tokens
    Consumable by every client which can speak
    HTTP(S)

    View full-size slide

  6. NODE.JS IS
    NOT
    JQUERY IN COOL ;-)

    View full-size slide

  7. NODE.JS - INTRO
    Server-side JavaScript powered by Chrome's V8
    JavaScript Engine
    Asynchronous, event-driven I/O API
    NuGet-like package manager: Node Package Manager
    (npm)
    Cross platform: OS X, Windows, Linux
    Upcoming: alternative runtime Microsoft's ChakraCore

    View full-size slide

  8. NODE.JS - FEATURE SUPPORT
    ECMAScript 6
    Experimental ECMAScript 7
    Classes
    Typed arrays
    Fat Arrow/Lambda
    Expressions
    Templated Strings

    View full-size slide

  9. NODE.JS - ASP.NET CORE 1.0
    Node.js ASP.NET Core 1.0
    JS, TS, CS, ...
    Node Package Manager
    Dynamic types
    Single threaded Event
    Loop
    but async I/O threads
    C#
    NuGet
    Static types
    Multi
    threaded

    View full-size slide

  10. NODE.JS - SERVER

    View full-size slide

  11. NODE.JS - PROS
    Full stack JS development
    Universal JavaScript
    Same code on client and server
    Built-in streaming
    Open Source loving
    community
    Enterprise proven
    Paypal, Netflix, Groupon

    View full-size slide

  12. NODE.JS - WATCH OUT
    Single threaded Event Loop
    Avoid heavy CPU usage via JS
    Utilizes one CPU only
    Scale via clustering
    Relational databases can be
    strange
    Code is documentation

    View full-size slide

  13. RESTIFY - OVERVIEW
    Node.js module to build Web
    APIs
    Middleware support (OWIN like)
    Client and server components
    Routing

    View full-size slide

  14. RESTIFY - MIDDLEWARE
    c
    o
    n
    s
    t s
    e
    r
    v
    e
    r = r
    e
    s
    t
    i
    f
    y
    .
    c
    r
    e
    a
    t
    e
    S
    e
    r
    v
    e
    r
    (
    )
    ;
    /
    / R
    u
    n
    s b
    e
    f
    o
    r
    e .
    u
    s
    e h
    a
    n
    d
    l
    e
    r
    s
    s
    e
    r
    v
    e
    r
    .
    p
    r
    e
    (
    (
    r
    e
    q
    , r
    e
    s
    , n
    e
    x
    t
    ) =
    > { /
    * D
    o s
    o
    m
    e
    t
    h
    i
    n
    g *
    / }
    )
    ;
    /
    / R
    u
    n
    s a
    f
    t
    e
    r .
    p
    r
    e h
    a
    n
    d
    l
    e
    r
    s
    s
    e
    r
    v
    e
    r
    .
    u
    s
    e
    (
    (
    r
    e
    q
    , r
    e
    s
    , n
    e
    x
    t
    ) =
    > { /
    * D
    o s
    o
    m
    e
    t
    h
    i
    n
    g *
    / }
    )
    ;
    /
    / S
    a
    m
    p
    l
    e
    : U
    s
    i
    n
    g a p
    r
    e
    d
    e
    f
    i
    n
    e
    d m
    i
    d
    d
    l
    e
    w
    a
    r
    e
    s
    e
    r
    v
    e
    r
    .
    u
    s
    e
    (
    r
    e
    s
    t
    i
    f
    y
    .
    b
    o
    d
    y
    P
    a
    r
    s
    e
    r
    (
    )
    )
    ;

    View full-size slide

  15. ASP.NET CORE 1.0 - MIDDLEWARE
    p
    u
    b
    l
    i
    c v
    o
    i
    d C
    o
    n
    f
    i
    g
    u
    r
    e
    S
    e
    r
    v
    i
    c
    e
    s
    (
    I
    S
    e
    r
    v
    i
    c
    e
    C
    o
    l
    l
    e
    c
    t
    i
    o
    n s
    e
    r
    v
    i
    c
    e
    s
    )
    {
    /
    / A
    d
    d a p
    r
    e
    d
    e
    f
    i
    n
    e
    d m
    i
    d
    d
    l
    e
    w
    a
    r
    e
    s
    e
    r
    v
    i
    c
    e
    s
    .
    A
    d
    d
    M
    v
    c
    (
    )
    ;
    }
    p
    u
    b
    l
    i
    c v
    o
    i
    d C
    o
    n
    f
    i
    g
    u
    r
    e
    (
    I
    A
    p
    p
    l
    i
    c
    a
    t
    i
    o
    n
    B
    u
    i
    l
    d
    e
    r a
    p
    p
    ,
    I
    H
    o
    s
    t
    i
    n
    g
    E
    n
    v
    i
    r
    o
    n
    m
    e
    n
    t e
    n
    v
    ,
    I
    L
    o
    g
    g
    e
    r
    F
    a
    c
    t
    o
    r
    y l
    o
    g
    g
    e
    r
    F
    a
    c
    t
    o
    r
    y
    )
    {
    /
    / U
    s
    e a p
    r
    e
    d
    e
    f
    i
    n
    e
    d m
    i
    d
    d
    l
    e
    w
    a
    r
    e
    a
    p
    p
    .
    U
    s
    e
    M
    v
    c
    (
    )
    ;
    }

    View full-size slide

  16. RESTIFY - ROUTING
    /
    / H
    T
    T
    P G
    E
    T
    s
    e
    r
    v
    e
    r
    .
    g
    e
    t
    (
    '
    /
    h
    e
    l
    l
    o
    '
    , (
    r
    e
    q
    , r
    e
    s
    ) =
    > r
    e
    s
    .
    s
    e
    n
    d
    (
    2
    0
    0
    , '
    H
    e
    l
    l
    o W
    o
    r
    l
    d
    '
    )
    )
    ;
    /
    / H
    T
    T
    P P
    O
    S
    T
    s
    e
    r
    v
    e
    r
    .
    p
    o
    s
    t
    (
    '
    /
    u
    p
    l
    o
    a
    d
    '
    , s
    o
    m
    e
    M
    i
    d
    d
    l
    e
    w
    a
    r
    e
    )
    ;
    /
    / H
    T
    T
    P P
    U
    T
    s
    e
    r
    v
    e
    r
    .
    p
    u
    t
    (
    '
    /
    c
    u
    s
    t
    o
    m
    e
    r
    /
    :
    i
    d
    '
    , m
    i
    d
    d
    l
    e
    w
    a
    r
    e
    1
    , m
    i
    d
    d
    l
    e
    w
    a
    r
    e
    2
    )
    ;
    /
    / H
    T
    T
    P D
    E
    L
    E
    T
    E
    s
    e
    r
    v
    e
    r
    .
    d
    e
    l
    (
    '
    /
    c
    u
    s
    t
    o
    m
    e
    r
    /
    :
    i
    d
    '
    , a
    n
    o
    t
    h
    e
    r
    M
    i
    d
    d
    l
    e
    w
    a
    r
    e
    )
    ;

    View full-size slide

  17. ASP.NET CORE 1.0 - ROUTING
    [
    H
    t
    t
    p
    G
    e
    t
    (
    "
    h
    e
    l
    l
    o
    "
    )
    ]
    p
    u
    b
    l
    i
    c I
    A
    c
    t
    i
    o
    n
    R
    e
    s
    u
    l
    t G
    e
    t
    (
    ) {
    r
    e
    t
    u
    r
    n O
    k
    (
    "
    H
    e
    l
    l
    o W
    o
    r
    l
    d
    "
    )
    ;
    }
    [
    H
    t
    t
    p
    P
    o
    s
    t
    (
    "
    u
    p
    l
    o
    a
    d
    "
    )
    ]
    p
    u
    b
    l
    i
    c I
    A
    c
    t
    i
    o
    n
    R
    e
    s
    u
    l
    t P
    o
    s
    t
    (
    ) { }
    [
    H
    t
    t
    p
    P
    u
    t
    (
    "
    c
    u
    s
    t
    o
    m
    e
    r
    /
    {
    i
    d
    }
    "
    )
    ]
    p
    u
    b
    l
    i
    c I
    A
    c
    t
    i
    o
    n
    R
    e
    s
    u
    l
    t P
    u
    t
    (
    i
    n
    t i
    d
    ) { }
    [
    H
    t
    t
    p
    D
    e
    l
    e
    t
    e
    (
    "
    c
    u
    s
    t
    o
    m
    e
    r
    /
    {
    i
    d
    }
    "
    )
    ]
    p
    u
    b
    l
    i
    c I
    A
    c
    t
    i
    o
    n
    R
    e
    s
    u
    l
    t D
    e
    l
    e
    t
    e
    (
    ) { }

    View full-size slide

  18. TOKEN
    AUTHENTICATION

    View full-size slide

  19. TOKEN AUTH - OVERVIEW
    Securing resources (APIs)
    OAuth2 Protocol
    Simple & standard method for web, mobile and desktop applications
    OpenId Connect
    Identity Layer on top of OAuth2

    View full-size slide

  20. TOKEN AUTH - NODE.JS
    Several node packages
    oauth2-server, oauth2orize, OpenIdConnect, node-oauth2-provider
    Currently no state-of-the-art package
    Implementation depends on the used
    package

    View full-size slide

  21. TOKEN AUTH - ASP.NET CORE 1.0
    State-of-the-art
    package
    IdentityServer4

    View full-size slide

  22. DATABASES - NODE.JS
    Adapters for lots of databases
    NoSQL databases
    MongoDB, CouchDB
    Relational databases
    MSSQL, PostgreSQL, MySQL, Sqlite
    ORM: SequelizeJS
    Multiple databases, transactions, migrations, replication, query API

    View full-size slide

  23. DATABASES - ASP.NET CORE 1.0
    Entity Framework 7
    Supports multiple databases
    MSSQL, Sqlite, InMemory, PostgreSQL
    Code First, Database First
    Migrations

    View full-size slide

  24. THANK YOU!
    GitHub repository
    https://github.com/thinktecture/nodejs-aspnetcore-webapi
    thinktecture.com

    View full-size slide

  25. RESOURCES
    GitHub repository:
    Node.js:
    Node.js at Paypal:
    Node.js at Netflix:
    Node.js at Walmart:
    Restify:
    SequelizeJS:
    ASP.NET Core 1.0:
    Entity Framework 7:
    IdentityServer4:
    Angular 2:
    https://github.com/thinktecture/nodejs-aspnetcore-webapi
    https://nodejs.org/
    https://www.paypal-engineering.com/?s=node
    http://techblog.netflix.com/search/label/node.js
    https://www.joyent.com/developers/videos/node-js-at-walmart-
    introduction
    http://restify.com/
    http://sequelizejs.com/
    https://docs.asp.net/
    http://docs.efproject.net/
    https://github.com/IdentityServer/IdentityServer4
    https://angular.io/

    View full-size slide