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

Web-APIs mit Node.js und TypeScript – leichtgewichtig und performant

Manuel Rauber
September 19, 2017

Web-APIs mit Node.js und TypeScript – leichtgewichtig und performant

Full-Stack TypeScript: Im Frontend werkeln moderne Single-Page-Application-Frameworks wie Angular oder React, während im Hintergrund leichtgewichtige Web-APIs zur Kommunikation arbeiten. Dank Node.js sprechen beide Welten eine gemeinsame Sprache: TypeScript – ein um Typen angereichertes Superset von JavaScript. Ob async/await, Klassen, statische Typisierung oder Generics – all das ist mit TypeScript möglich. Lassen Sie uns gemeinsam einen Blick in die Node.js-Welt mit TypeScript werfen und dabei Aspekte von modernen Web-APIs, wie die Anbindung von Datenbanken oder leichtgewichtige Architekturen, beleuchten. Mit wenigen Zeilen Code schafft Manuel Rauber von Thinktecture eine Grundlage für Ihr „Next Generation“ Web-API.

GitHub: https://github.com/thinktecture/api-conference-2017-nodejs-typescript-webapi

Manuel Rauber

September 19, 2017
Tweet

More Decks by Manuel Rauber

Other Decks in Programming

Transcript

  1. Web-APIs mit Node.js und TypeScript
    Leichtgewichtig & Performant
    Manuel Rauber
    @manuelrauber
    Consultant

    View Slide

  2. Consultant @ Thinktecture AG
    ! [email protected]
    " @manuelrauber
    # https://manuel-rauber.com
    Microsoft MVP
    The guy who’s talkin’
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Manuel Rauber

    View Slide

  3. Web APIs
    Node.js
    TypeScript
    Restify
    Database
    Deployment
    Examples
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Talking Points

    View Slide

  4. Overview
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Web APIs
    Web API
    Node.js/.NETCore
    Browser
    GET /api/customer/1
    Authorization: Bearer
    File system
    PostgreSQL
    Elasticsearch
    validate token
    get token
    STS
    1
    2
    3
    4

    View Slide

  5. Overview
    • REST APIs, HTTP APIs, “whatever APIs”
    • Use case oriented: model functional services
    with dedicated interfaces
    • Can use several other services
    for data aggregation
    • Secured by tokens
    • Consumable by every HTTPS speaking client
    • Can involve WebSocket for real-time
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Web APIs
    HTTP HTTPS WebSocket
    Service A Service B Service C
    Web APIs
    (ASP.NET, Node.js, …)
    AuthN & AuthZ
    Client

    View Slide

  6. Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Node.js is
    not
    jQuery in cool ;-)
    Node.js

    View Slide

  7. Intro
    • Server-side JavaScript powered by Chrome’s V8 JavaScript engine
    • Asynchronous, event-driven I/O API
    • Package management via Node Package Manager (npm) or YARN
    • Cross platform: Linux, macOS, Windows
    • Work in progress: Microsoft’s ChakraCore as an alternative runtime
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Node.js

    View Slide

  8. Features
    • ECMAScript 6
    • Experimental ECMAScript 7
    • http://node.green/
    • Classes
    • Typed arrays
    • Fat Arrow/Lambda Expressions
    • Templated Strings
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Node.js

    View Slide

  9. Server Architecture
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Node.js

    View Slide

  10. Pros
    • Full stack JS’ish development
    • Universal JavaScript: Share code between client & server
    • Open Source loving community
    • Enterprise proven: Netflix, Paypal, Groupon, Walmart, …
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Node.js
    https://nodejs.org/static/documents/casestudies/Node_CaseStudy_Nasa_FNL.pdf

    View Slide

  11. Watch out!
    • Single threaded Event Loop: Avoid heavy CPU usage
    • Utilizes one CPU only: Scale via clustering
    • Relational databases can be strange – but it’s getting better!
    • Code is (often the only) documentation
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Node.js

    View Slide

  12. • Designed by Anders Heijsberg, who also designed C#
    • Typed superset of JavaScript that transpiles to plain JavaScript
    • Types are optional, but useful metadata for tooling
    • Static Code Analysis
    • Refactoring
    • Linting
    • IntelliSense
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    TypeScript

    View Slide

  13. • Node.js module to build Web APIs
    • Middleware support
    • Client & server components
    • Routing
    • Useful plugins included
    • Used by Netflix, reddit, npm
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Restify
    Middleware 1
    // Server logic
    next()
    // more logic
    Middleware 2
    // Server logic
    next()
    // more logic
    Middleware 3
    // Server logic
    // more logic
    Request
    Response
    Client
    Logging Authentication …

    View Slide

  14. • Adapters for a lot of databases
    • NoSQL databases
    MongoDB, CouchDB, …
    • Relational databases
    MSSQL, PostgreSQL, MySQL, SQLite, …
    • TypeORM as a type-safe ORM, but currently in alpha state
    Multiple databases, transactions, migrations, query API
    • Alternative: SequelizeJS, more mature
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Databases

    View Slide

  15. • Dockerize it!
    • Deployable via Docker containers on
    • Microsoft Azure: Container Registry, Container Services, Container
    Instances
    • IBM Bluemix Container Services
    • Amazon EC2 Container Services
    • Google Cloud Platform
    • Runnable via
    • Simple Azure Linux-based Web Apps
    • Docker Swarm
    • Kubernetes
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Deployment

    View Slide

  16. Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    DEMO

    View Slide

  17. The unspoken things of the talk
    • API Rate Limiting/Throttling
    • Versioning
    • Caching
    • Gzipping
    • Content Negotiation
    • Cross-origin resource sharing (CORS)
    • Documentation
    • API Discovery
    • Real-time communication via WebSocket (e.g. Socket.io)
    • Security
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    That’s all?

    View Slide

  18. • Node.js overview
    Feature support, Node.js server, pros, watch outs
    • Web APIs with restify
    Routing, middlewares
    • Relational databases via TypeORM
    Models, query API
    • A lot more to discover
    Real-time communication, security, throttling, rate limitting
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Summary

    View Slide

  19. eBook & entwickler.de API special
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Read on!
    https://entwickler.de/press/shortcuts/die-serverwelt-von-node-js-2-262553.html
    https://entwickler.de/entwickler-magazin/entwickler-magazin-spezial-vol-13-apis-579812053.html

    View Slide

  20. • Node.js: https://nodejs.org/
    • Node.js at Paypal: https://www.paypal-engineering.com/tag/node-js/
    • Node.js at Netflix: http://techblog.netflix.com/search/label/node.js
    • Node.js at scale: http://venturebeat.com/2012/01/24/node-at-google-mozilla-
    yahoo/
    • Restify: http://restify.com/
    • TypeORM: https://typeorm.github.io/
    • SequelizeJS: http://sequelizejs.com/
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    API Conference 2017
    Resources

    View Slide

  21. Thank you! Questions?
    Repository
    Web-APIs mit Node.js und TypeScript - leichtgewichtig und performant
    https://github.com/thinktecture/api-conference-2017-nodejs-typescript-webapi
    Contact
    @manuelrauber
    [email protected]

    View Slide