Slide 1

Slide 1 text

BUILDING AN API WITH VISUAL STUDIO CODE AND NODE.JS Jeff Linwood Austin Microsoft Developer Meetup April 20, 2016 www.jefflinwood.com

Slide 2

Slide 2 text

OVERVIEW •Software we are using •Visual Studio Code – what is it? •Intro to VS Code •Intro to Node.js •Deploying to Azure App Services •Visual Studio Code - Node.js Debugger •Building an API with HAPI

Slide 3

Slide 3 text

SOFTWARE WE ARE USING •Visual Studio Code (http://code.visualstudio.com) •Node.js (https://nodejs.org/en/download/) •Azure Command Line Interface (https://azure.microsoft.com/en- us/documentation/articles/xplat-cli-install/) •Will need to have an Azure account to deploy •Connect your Azure account to the Command Line Interface

Slide 4

Slide 4 text

INTRO TO VISUAL STUDIO CODE •Visual Studio Code vs Visual Studio •Windows, OS X, Linux •Version 1.0 just came out last week! •Open Source - https://github.com/Microsoft/vscode •Many languages •Extensions

Slide 5

Slide 5 text

INTRO TO NODE JS •We’ll be using Node.js today •What is Node.js? •How might we think of development differently using Node instead of other technologies? •Node Package Manager – NPM •https://www.npmjs.com/

Slide 6

Slide 6 text

BUILDING WEB APPS WITH NODE.JS •Many different web frameworks to use •Most common is Express (http://expressjs.com/) – others include Koa and Sails.js •Let’s make a Hello World app using express! •Create an empty directory (and open it in Code) •md hello •cd hello

Slide 7

Slide 7 text

NPM INIT •Setup the project using npm init – accept the defaults EXCEPT make the entry point should be app.js NOT index.js •Azure’s command line tool will rely on app.js or server.js existing to set up iisnode.yml, which Azure App Services uses to connect IIS to your Node.js app

Slide 8

Slide 8 text

ADDING EXPRESS •The npm init directions give you an example of how to add a module, like express: •npm install express –save •See the change in package.json – with the dependencies •Semantic Versioning – major.minor.patch •Caret – newer minor and patch •Tilde – newer patch

Slide 9

Slide 9 text

LET’S WRITE SOME CODE! •Create an app.js file with these contents var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!'); }); app.listen(process.env.port || 3000, function () { console.log('We are listening!'); });

Slide 10

Slide 10 text

NOW LET’S RUN IT! •node index.js •Visit http://localhost:3000/ in your web browser •Ok, so that was pretty basic •Let’s take it to the next level and run this on Azure!

Slide 11

Slide 11 text

SETTING UP AZURE •You’ll need an Azure account (for instance, Pay As You Go) •You need to create an app •You’ll also need to set up git credentials for deployment •https://azure.microsoft.com/en- us/documentation/articles/web-sites-publish- source-control/

Slide 12

Slide 12 text

CREATING AN AZURE SITE •Once you have your git deployment credentials set in Azure •Only enter them once on Windows if you want •git config --global credential.helper wincred •Azure site create --git •I bet HelloWorld is taken, so be creative J

Slide 13

Slide 13 text

BROWSING YOUR NEW SHINY NODE SITE •Couldn’t be easier with Azure •azure site browse

Slide 14

Slide 14 text

USING THE DEBUGGER IN VS CODE •Click the little Bug on the right hand side •Click Settings •Pick Node.js as the environment •Click the little Green Play Icon! •Up and running with the Debugger •Set a breakpoint in app.js •Open http://localhost:3000/

Slide 15

Slide 15 text

RESTFUL API DESIGN •REST - Representational state transfer •API models a set of resources •Uses native HTTP verbs – GET, POST, PUT, DELETE •Similar to old CRUD model of app development – Create, Retrieve, Update, Destroy •Probably closely linked to your database design, especially if building from scratch •Supported by modern web development frameworks such

Slide 16

Slide 16 text

JSON VS XML •Typically, new web service APIs use JSON to format data •JSON is the Javascript Object Notation, and is a standard for transmitting data •XML is an older format •Not everything supports both JSON and XML •Most developers will prefer JSON support

Slide 17

Slide 17 text

CREATING AN API WITH NODE.JS AND HAPI •Let’s start a new application •Create a directory named api •npm init •We’ll also call the default Javascript file app.js •We’ll use hapi (http://hapijs.com/) instead of express •npm install --save hapi

Slide 18

Slide 18 text

THE CODE FOR THE API – APP.JS •In this Gist: •https://gist.github.com/jefflinwood/df0de3d289 79097f3d85bd0af9d842d5 •Run it – http://localhost:3000/ and http://localhost:3000/meetups •You can also run it in Edge’s F12 Developer Tools to see the headers (application/json)

Slide 19

Slide 19 text

MORE READING •More complicated Express App on Azure •https://azure.microsoft.com/en- us/documentation/articles/app-service-web- nodejs-get-started/

Slide 20

Slide 20 text

MY CONTACT INFO •Email me at [email protected] •Twitter: https://twitter.com/jefflinwood •http://www.jefflinwood.com/ •http://www.biscottilabs.com/