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.
•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
is Node.js? •How might we think of development differently using Node instead of other technologies? •Node Package Manager – NPM •https://www.npmjs.com/
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
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
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
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/
credentials set in Azure •Only enter them once on Windows if you want •git config --global credential.helper wincred •Azure site create --git <MyAppName> •I bet HelloWorld is taken, so be creative J
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/
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
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
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
•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)