Node.js Website with AzureAD & Office 365 Integration
The Scenario - End User
This sample simply uses a simple website that shows how to authenticate with AzureAD, obtain access tokens and query different Office 365 services via the REST APIs.
The site uses sessions to persist the currently logged in user and store credential information for specified endpoints (ie: access tokens, resource IDs and endpoints for Office 365 and Azure endpoints). The sessions are stored in JSON files located in the
/sessions folder. This is not a great production practice, rather you should look at some of the packages for using something like Redis, Mongo or others. The file store is used to easily view the session contents.
Developer Application Requirements
This sample can be run from any platform (Windows, OS X or Linux) and use any developer IDE of choice (Visual Studio, WebStorm, Brackets, notepad). You do need Node.js installed and the following packages must be installed globally on your environment:
Install these globally using the following command:
npm install -g bower express typescript tsd
Furthermore you can install the following additional packages globally if you want to run the build process:
First obtain all external references, packages & type definitions by running the following command:
This will (1) download all NPM packages referenced in the project, (2) download all bower packages and (3) download all TypeScript type definitions. Stes 2 & 3 are done after
npm install completes as dictated by a script in the
package.json file. You can run them manually using
bower install and
tsd reinstall if you wanted.
Compile the TypeScript, used in Node.js application, by running the following command from within the root of the application:
When developing you can run the following. This will monitor any changes to TypeScript and handlebars files (used as the MVC view engine in the Node.js application). If any changes are detected it will automatically do the following things:
- lint all TypeScript (gulp task lint): This will check all TypeScript to ensure it meets the coding style rules defined in the tslint.json file.
- update TypeScript references (gulp task gen-server-tsrefs): There are three files referenced at the top of every TypeScript file. The first, /tools/typings/tsd.d.ts, contains references to all TypeScript type definition files obtained using the tool tsd.exe. The other other file that's referenced is /tools/typings/server.d.ts... it references the TypeScript files used within the application. This saves the developer from adding all these references to each and every file in the project.
Running the Application
The application, as it stands in the GitHub repo, is configured to run locally.
Developing with WebStorm
This sample was built using WebStorm, but you can use any IDE you want. The way I developed it was to open a Terminal / command window and run
gulp which did all the compilation and updates to the app and then monitored it for changes. I also created a new run configuration which is shown in this blog post: Running and debugging Node.js application. For the specific settings in my configuration:
- Node parameters: --debug
- only necessary if you want to debug with breakpoints
- Working directory: (path to this folder)
Developing with Anything Else
You can use any tool you want to customize this application, including Visual Studio provided you have the Node.js tools for Visual Studio installed.
If you use any other text editor, you can setup debugging. First turn on node-inspector for rich Node.js debugging using the browser's developer tools:
Then run the following gulp task that will do the same watching, linting & recompilation that I mentioned above, except it will also automatically restart the Node.js process to get the latest code changes: