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

Modern Web APIs with Node.js & TypeScript

Modern Web APIs with Node.js & TypeScript

Full-Stack-Development! Modern frontends are using Single-Page Applications like Angular or React communicating with some light-weight Web APIs in the background. Thanks to Node.js we can speak one language in both worlds: JavaScript … or even TypeScript! Within this session we want to take a look at building (lots of live coding!) modern and light-weight Web APIs based on Node.js with TypeScript and connect to a database using an ORM called TypeORM. A few lines of code are needed to create a “next generation” Web API.

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

Manuel Rauber

October 24, 2017
Tweet

More Decks by Manuel Rauber

Other Decks in Programming

Transcript

  1. Modern Web APIs
    Node.js & TypeScript
    Manuel Rauber
    @manuelrauber
    Consultant

    View Slide

  2. Consultant @ Thinktecture AG
    ! [email protected]
    " @manuelrauber
    # https://manuel-rauber
    .com
    Microsoft MVP
    The guy who’s talkin’
    Modern Web APIs with Node.js & TypeScript
    International JavaScript Conference 2017
    Manuel Rauber

    View Slide

  3. Web APIs
    Node.js
    TypeScript
    Restify
    Database
    Deployment
    Examples
    Modern Web APIs with Node.js & TypeScript
    International JavaScript Conference 2017
    Talking Points

    View Slide

  4. Overview
    Modern Web APIs with Node.js & TypeScript
    International JavaScript 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
    Modern Web APIs with Node.js & TypeScript
    International JavaScript 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. Node.js
    Node.js is
    not
    jQuery in cool ;-)
    Modern Web APIs with Node.js & TypeScript
    International JavaScript Conference 2017

    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
    Modern Web APIs with Node.js & TypeScript
    International JavaScript 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
    Modern Web APIs with Node.js & TypeScript
    International JavaScript Conference 2017
    Node.js

    View Slide

  9. Server Architecture
    Modern Web APIs with Node.js & TypeScript
    International JavaScript 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, …
    Modern Web APIs with Node.js & TypeScript
    International JavaScript 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
    Modern Web APIs with Node.js & TypeScript
    International JavaScript Conference 2017
    Node.js

    View Slide

  12. • Designed by Anders Hejlsberg, 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
    Modern Web APIs with Node.js & TypeScript
    International JavaScript 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
    Modern Web APIs with Node.js & TypeScript
    International JavaScript 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
    Modern Web APIs with Node.js & TypeScript
    International JavaScript 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
    Modern Web APIs with Node.js & TypeScript
    International JavaScript Conference 2017
    Deployment

    View Slide

  16. Modern Web APIs with Node.js & TypeScript
    International JavaScript 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
    Modern Web APIs with Node.js & TypeScript
    International JavaScript 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 limiting
    Modern Web APIs with Node.js & TypeScript
    International JavaScript Conference 2017
    Summary

    View Slide

  19. eBook & entwickler
    .de API special
    Modern Web APIs with Node.js & TypeScript
    International JavaScript 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/
    Modern Web APIs with Node.js & TypeScript
    International JavaScript Conference 2017
    Resources

    View Slide

  21. Thank you! Questions?
    Repository
    Modern Web APIs with Node.js & TypeScript
    https://github.com/thinktecture/javascript-conference-2017-nodejs-typescript
    Contact
    @manuelrauber
    [email protected]

    View Slide