Building an API With Visual Studio Code and Node.js
I gave this presentation at the Austin Microsoft Developer's Meetup on April 20, 2016 - a quick beginner's guide to using Visual Studio Code with Node.js, and how to deploy those Azure web applications to Azure App Services.
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
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
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
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/
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
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
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
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!'); });
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!
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/
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
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/
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
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
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
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)