Friday, May 31, 2019

Blazor client-side app with CRUD operations against a Web API endpoint

A companion video for this article is: https://www.youtube.com/watch?v=wkSR3eo4Tek

Source code for this application can be found at:
https://github.com/medhatelmasry/BlazingSchool

Overview

What is Blazor?
Blazor is a framework for developing interactive client-side web applications using C# instead of JavaScript.

What are we going to do in this Tutorial?In this tutorial I will show you how to build a Blazor application with the following structure:
1) The class library will have model definitions that are shared between the Web API and Blazor Visual Studio projects. The model that we will create in this tutorial is a simple C# Student model that looks like this:

public class Student {
  public string StudentId { get; set; }
  [Required]
  public string FirstName { get; set; }
  [Required]
  public string LastName { get; set; }
  [Required]
  public string School { get; set; }
}

2) The ASP.NET Core Web API app will provide the REST endpoints for the students service that the Blazor client-side application will consume. It will use the GET, POST, PUT and DELETE methods to carry out CRUD operations with the API service. Entity Framework will be used to save data in SQL Server using the "Code First" approach.

3) The Client-side Blazor app will provide the user interface for processing student data.

Perquisites

This tutorial was written while Blazor was in preview mode. I used the following site for setting up my environment:

This is how I setup my development environment:

- .NET Core 3.0 Preview SDK installed from https://dotnet.microsoft.com/download/dotnet-core/3.0

- Install Blazor templates by running the following command in a terminal window:

dotnet new -i Microsoft.AspNetCore.Blazor.Templates::3.0.0-preview5-19227-01

- Visual Studio 2019 preview version installed from: https://visualstudio.com/preview

- Latest Blazor extension for Visual Studio 2019 installed from https://go.microsoft.com/fwlink/?linkid=870389.

Creating the class library

Create a new project in Visual Studio 2019.

Choose "Class Library (.NET Standard)" then click Next:

Set these values on the next dialog:

Project name: SchoolLibrary
Solution name: School

Click on Create.

We need to install a package to the class library project. From within a terminal window at the main SchoolLibrary folder, run the following command:

dotnet add package System.ComponentModel.Annotations

Delete Class1.cs, then create a new Student.cs class file and add to it the following code:
public class Student {
  public string StudentId { get; set; }
  [Required]
  public string FirstName { get; set; }
  [Required]
  public string LastName { get; set; }
  [Required]
  public string School { get; set; }
}

Resolve "Required" by adding the following using statement at the top of Student.cs:
using System.ComponentModel.DataAnnotations;

Creating the ASP.NET Core Web API student service application

Right-click on the main solution node in Solution Explorer and select: Add >> New Project. Choose the "ASP.NET Core Web Application" template:
Name the Project name: SchoolAPI

Click on Create.

On the next dialog, select ASP.NET Core 2.2 and choose the API template then click on Create:

Our Web API project needs to use the Student class in the SchoolLibrary project. Therefore, we will make a reference from the SchoolAPI project to the SchoolLibrary project. Right-click on the SchoolAPI project node then: Add >> Reference... >> Projects >> Solution >> check SchoolLibrary. Then click on OK.


We need to let the dotnet utility know that we are using .NET Core 2.2 in the SchoolAPI project, even though the default SDK on our computer is a preview version of .NET Core 3.0. This is done by adding a global.json file in the root SchoolAPI folder with the following content:
{
  "sdk": {
    "version": "2.2.103"
  }
}

A quick way of doing this is to run the following CLI command inside the SchoolAPI folder:
dotnet new globaljson --sdk-version 2.2.103

Since we will be using SQL Server, we will need to add the appropriate Entity Framework packages and tooling. From within a terminal window at the root of your StudentAPI project,  run the following commands that will import the appropriate database related packages:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.SqlServer.Design
dotnet add package Microsoft.EntityFrameworkCore.Tools

We need to add a connection string to the database. Add the following to the top of the appsettings.json file in project SchoolAPI:
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=SchoolDB;Trusted_Connection=True;MultipleActiveResultSets=true"
  },

We will be using the Entity Framework Code First approach. The starting point is to create a database context class. Add a C#  class file named SchoolDbContext.cs with the following class code:
public class SchoolDbContext : DbContext {
  public DbSet<Student> Students { get; set; }

  public SchoolDbContext(DbContextOptions<SchoolDbContext> options) : base(options) { }

  protected override void OnModelCreating(ModelBuilder builder) {
    base.OnModelCreating(builder);
    
    builder.Entity<Student>().HasData(
      new { StudentId = Guid.NewGuid().ToString(), 
            FirstName = "Jane", LastName = "Smith", School = "Medicine" },
      new { StudentId = Guid.NewGuid().ToString(), 
            FirstName = "John", LastName = "Fisher", School = "Engineering" },
      new { StudentId = Guid.NewGuid().ToString(), 
            FirstName = "Pamela", LastName = "Baker", School = "Food Science" },
      new { StudentId = Guid.NewGuid().ToString(), 
            FirstName = "Peter", LastName = "Taylor", School = "Mining" }
    );
  }
}

You will need to resolve some of the unrecognized namespaces.

Notice the above code is adding four records of seed data into the database.

In the Startup.cs file in the SchoolAPI project, add the following code to the ConfigureServices() method so that our application can use SQL Server:

services.AddDbContext<SchoolDbContext>(
  option => option.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
We are now ready to apply Entity Framework migrations, create the database and seed data. Remember to build your entire solution before proceeding. Then, from within a terminal window in the SchoolAPI directory, run the following command to create migrations:
dotnet ef migrations add initial_migration

This results in the creation of a migration file ending with the name ....initial_migration.cs in the Migrations folder. In my case, this file looked like this:
using Microsoft.EntityFrameworkCore.Migrations;
namespace SchoolAPI.Migrations {
  public partial class initial_migration : Migration {
    protected override void Up(MigrationBuilder migrationBuilder) {
      migrationBuilder.CreateTable(
        name: "Students",
        columns: table => new {
          StudentId = table.Column<string>(nullable: false),
          FirstName = table.Column<string>(nullable: false),
          LastName = table.Column<string>(nullable: false),
          School = table.Column<string>(nullable: false)
        },
        constraints: table => {
          table.PrimaryKey("PK_Students", x => x.StudentId);
        });

      migrationBuilder.InsertData(
        table: "Students",
        columns: new[] { "StudentId", "FirstName", "LastName", "School" },
        values: new object[,] {
          { "8734ce0b-4337-487a-bd86-102130d47f8d", "Jane", 
            "Smith", "Medicine" },
          { "db356c3a-d745-4f65-9aac-234f706859c2", "John", 
            "Fisher", "Engineering" },
          { "7721c620-a82b-4204-80af-6c7636efc81e", "Pamela", 
            "Baker", "Food Science" },
          { "9a85f51f-160e-4003-9de9-b01354e180a3", "Peter", 
            "Taylor", "Mining" }
        });
    }

    protected override void Down(MigrationBuilder migrationBuilder) {
      migrationBuilder.DropTable(
        name: "Students");
    }
  }
}

Note that the above code also includes commands for inserting sample data.

The next step is to create the SchoolDB database in SQL Server. This is done by running the following command from inside a terminal window at the SchoolAPI folder.

                                dotnet ef database update


If no errors are encountered, we can assume that the database was created and properly seeded with data. Let us create an API controller so that we can see the data that is in our database.  

Right-click on the Controllers node in the SchoolAPI project then: Add >> Controller...

Under Installed >> Common, choose the API tab, highlight "API Controller with actions, using Entity Framework" then click Add.



On the next dialog, make the following choices:

Model class: Student (SchoolLibrary)
Data context class: SchoolDbContext (SchoolAPI)
Controller name: StudentsController


Once you click on Add, the StudentsController.cs file is created in the Controllers folder. To load that particular controller every time you run the SchoolAPI application, edit the launchSettings.json file under the Properties node. Change every instance of "api/values" to "api/students". Now you can run the application by hitting CTRL F5 on your keyboard. This is what you should see in your browser:

[{"studentId":"7721c620-a82b-4204-80af-6c7636efc81e",
  "firstName":"Pamela","lastName":"Baker","school":"Food Science"},
{"studentId":"8734ce0b-4337-487a-bd86-102130d47f8d",
  "firstName":"Jane","lastName":"Smith","school":"Medicine"},
{"studentId":"9a85f51f-160e-4003-9de9-b01354e180a3",
  "firstName":"Peter","lastName":"Taylor","school":"Mining"},
{"studentId":"db356c3a-d745-4f65-9aac-234f706859c2",
  "firstName":"John","lastName":"Fisher","school":"Engineering"}]

Even though our API app seems to be working fine, there is one more thing we need to do. We need to enable CORS (Cross Origin Resource Sharing) so that the service can be accessed from other domains. Add the following code to the ConfigureServices() method in the Startup.cs file found in the SchoolAPI project:

// Add Cors
services.AddCors(o => o.AddPolicy("Policy", builder => {
  builder.AllowAnyOrigin()
    .AllowAnyMethod()
    .AllowAnyHeader();
}));

Add this statement in the Configure() method also in Startup.cs just before app.UseMvc():

            app.UseCors("Policy");

We now have an API and data that we can work with. Let us now see how we can use this data in a Blazor app.

Creating our Client-Side Blazor app

Let us add a client-side Blazor project to our solution. In Solution Explorer, right-click on the Solution node then: Add >> New Project...

Choose the "ASP.NET Core Web Application" template then click on Next.


Name the project SchoolClient, then click on Create.

Choose "ASP.NET Core 3.0" and the "Blazor (client-side)" template then click on Create.

Let us run our Blazor application to see what we have out of the box. Run the SchoolClient application by hitting CTRL F5 on your keyboard. You will see a UI that looks like this:

We will add a Students razor page and menu item to this client-side Blazor application.

Our Blazor project needs to use the Student class in the class library. Therefore, we will make a reference from the Blazor SchoolClient project to the class library project SchoolLibrary. Right-click on the SchoolClient project node then: Add >> Reference... >> Projects >> Solution >> check SchoolLibrary. Then click OK.


Open the _Imports.razor file in the editor and add the following using statement to the bottom of the content so that the Student class is available to a  views:

@using SchoolLibrary

Make a duplicate copy of the FetchData.razor file in the Pages node and call the new file Students.razor. Replace its contents with the following code:

@page "/students"
@inject HttpClient httpClient

<h1>Students</h1>

<p>This component demonstrates fetching data from the server.</p>

@if (students == null) {
<p><em>Loading...</em></p>
} else {
<table class='table table-hover'>
  <thead>
    <tr>
      <th>ID</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>School</th>
    </tr>
  </thead>
  <tbody>
    @foreach (var item in students) {
    <tr>
      <td>@item.StudentId</td>
      <td>@item.FirstName</td>
      <td>@item.LastName</td>
      <td>@item.School</td>
    </tr>
    }
  </tbody>
</table>
}

@functions {
  Student[] students;
  string baseUrl = "https://localhost:44318/";

  protected override async Task OnInitAsync() {
    await load();
  }

  protected async Task load() {
    students = await httpClient.GetJsonAsync<Student[]>($"{baseUrl}api/students");
  }
}

NOTE: Make sure you adjust the value of baseUrl to match the URL of your SchoolAPI service.

Let us focus on the @functions block. The OnInitAsyns() method is called when the page gets loaded. It calls a local load() method. The load() method loads a students array with data from our SchoolAPI service. The remaining HTML/Razor code simply displays the data in a table.

Let us add a menu item to the left-side navigation of our client application. Open Shared/NavMenu.razor in the editor and add the following <li> to the <ul> block (around line 24):

<li class="nav-item px-3">
  <NavLink class="nav-link" href="students">
    <span class="oi oi-list-rich" aria-hidden="true"></span> Students
  </NavLink>
</li>

You must be looking forward to testing our client-side application. To run your app, highlight the SchoolAPI node then hit CTRL F5 to run the server-side application without debugging. This starts the API service.

Next, we will run the client-side Blazor application. Highlight the SchoolClient node then hit CTRL F5 to run the blazor app. This is what it should look like when you click on Students:


Adding data using the POST method
The client side application will not be complete without adding to it add, edit and delete funtionality. We shall start with adding data. Place the following instance variables just above the OnInitAsyc() method:

string studentId;
string firstName;
string lastName;
string school;

We need an HTML form to add data. Add the following RAZOR code just before @functions:

@if (students != null) // Insert form
{
  <input placeholder="First Name" bind="@firstName" /><br />
  <input placeholder="Last Name" bind="@lastName" /><br />
  <input placeholder="School" bind="@school" /><br />
  <button onclick="@Insert" class="btn btn-warning">Insert</button>
}

When the Insert button is clicked, an Insert() method is called. Add the following Insert() method inside the @functions() block that makes a POST request to the API service:
protected async Task Insert() {
  string endpoint = $"{baseUrl}api/students";

  Student student = new Student() {
    StudentId = Guid.NewGuid().ToString(),
    FirstName = firstName,
    LastName = lastName,
    School = school
  };

  await httpClient.SendJsonAsync(HttpMethod.Post, endpoint, student);
  ClearFields();
  await load();
  StateHasChanged(); // causes the page to get automatically refreshed
}

After data is added, the above code clears the fields then loads the data again in the HTML table. Add the following ClearFields() method:

protected void ClearFields() {
  studentId = string.Empty;
  firstName = string.Empty;
  lastName = string.Empty;
  school = string.Empty;
}

Run the Blazor client-side application and select Students from the navigation menu. This is what it should look like:



I entered David, Strong and Entertainment for data and when I clicked on Insert I got the following data added to the database:
Updating & Deleting data using the PUT & DELETE methods
To distinguish between INSERT and EDIT/DELETE mode, we shall add an enum to our code. Add the following to the list of instance variables:

private enum MODE { None, Add, EditDelete };
MODE mode = MODE.None;
Student student;

We will add a button at the top of our table dedicated to adding data. Add the following markup just above the opening <table> tag:

<button onclick="@Add"  class="btn btn-success">Add</button>

Let us add the following Add() method:

protected void Add() {
  ClearFields();
  mode = MODE.Add;
}

Around line 34, change the @if (students != null) statement to:

@if (students != null && mode==MODE.Add)

If you run the application, the form for inserting data only displays when the Add button is clicked:
After we successfully insert data, we want the insert form to disappear. Add the following code to the bottom of the Insert() method:
mode = MODE.None;
Let us now add a form that only appears when we wish to update or delete data. Add the following just before @functions:

@if (students != null && mode==MODE.EditDelete) // Update & Delete form
{
  <input type="hidden" bind="@studentId" /><br />
  <input placeholder="First Name" bind="@firstName" /><br />
  <input placeholder="Last Name" bind="@lastName" /><br />
  <input placeholder="School" bind="@school" /><br />
  <button onclick="@Update" class="btn btn-primary">Update</button>
  <span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
  <button onclick="@Delete" class="btn btn-danger">Delete</button>
}

Add these Update() and Delete() methods as shown below:

protected async Task Update() {
  string endpoint = $"{baseUrl}api/students/{studentId}";

  Student student = new Student() {
    StudentId = studentId,
    FirstName = firstName,
    LastName = lastName,
    School = school
  };

  await httpClient.SendJsonAsync(HttpMethod.Put, endpoint, student);
  ClearFields();
  await load();
  mode = MODE.None;
  StateHasChanged(); // causes the page to get automatically refreshed
}

protected async Task Delete() {
  string endpoint = $"{baseUrl}api/students/{studentId}";
  await httpClient.SendJsonAsync(HttpMethod.Delete, endpoint, null);
  ClearFields();
  await load();
  mode = MODE.None;
  StateHasChanged(); // causes the page to get automatically refreshed
}

We want to be able to select a row of data and update it. We can add an onclick handler to a row. In the HTML table, replace the <tr> opening tag under the @foreach statement with this:

<tr onclick="@(() => Show(item.StudentId))">

The above would pass the appropriate studentId to a method named Show() whenever a row is clicked. Add the following Show() method that retrieves a student from the API service and displays it in the Update/Delete form:

protected async Task Show(string id) {
  student = await httpClient.GetJsonAsync<Student>($"{baseUrl}api/students/{id}");
  studentId = student.StudentId;
  firstName = student.FirstName;
  lastName = student.LastName;
  school = student.School;
  mode = MODE.EditDelete;
}

Let us run the application and test it out for adding, updating and deleting data. Run the application:
Click on the Add button to insert data. 
Enter data then click on the Insert button. The data should get added to the database:

To update the data, click on a row. The selected data should display in the update/delete form.

I selected Bill Black and changed Black to White then clicked on Update. Last Name was successfully changed to White.

Lastly, let us delete a record. I clicked on the Bill White row. Data was displayed in the Update/Delete form.
When I clicked on Delete, Bill White get deleted.
I hope you found this tutorial useful. I expect Blazor to change by the time it is generally available in release mode. I will do my best to update this article with any breaking changes.

Thursday, March 28, 2019

Inspecting your .NET classes with .NET Object Model Diagram Generator

OmgGenerator


While at the MVP summit in March 2019, I attended a short presentation by Morten Nielson, a fellow MVP. He talked about a utility he created that facilitates inspection & documentation of .NET classes. You can visit his GitHub project at https://github.com/dotMorten/DotNetOMDGenerator/blob/master/README.md.

There is much more detail about his utility at Morton's GitHub site. I will, however, give you a very stripped down version of what this utility is capable of doing in this article. To start with, download and globally install the OmgGenerator with:


dotnet tool install --global dotMorten.OmdGenerator


Once you have installed the OmgGenerator utility, you can inspect .NET classes in your application by running the following command in the root of your .NET project:

generateomd /source=. /output=diagram

I run the above commamd in one of my simple projects and this is what was created in a file named diagram.html:

 
Thev default output format is HTML. You can generate the output in markdown format with the /fotmat switch as shown below:

generateomd /source=. /output=diagram /format=md

The above would produce a file named diagram.md with the same diagram as before.
You can get the utility to exclude certain directories with the /exclude switch. The following would exclude folders bin, tests, Views and Areas:

generateomd /source=. /output=diagram /exclude="*/bin/*;*/tests/*;*/Views/*;*/Areas/*"

Another very powerful feature is the tool’s ability to identify the class differences between two branches in GitHub. In the following example I am comparing the master branch and the ver2 branch in a GitHub repo:

generateomd /output=difs /source=https://github.com/medhatelmasry/HealthAPI/archive/ver2.zip /compareSource=https://github.com/medhatelmasry/HealthAPI/archive/master.zip /exclude="*/ref/*;*/tests/*;*/perftests/*"

The diff.html file produced looks like this:

The above tells me the following:
  • the Symptoms property was added to the Ailment class
  • a new Hospital class was added
  • the Supplier property was added to the Medication class

Conclusion

I am impressed with Morten's tool and would recommend it to any .NET programmer out there.


Wednesday, February 13, 2019

Deploy Node Express Docker Container to Azure

In this tutorial I will show you how to easily deploy a simple Node-Express docker image and run it from Azure containers. It is assumed that you have:
  • Docker installed on your computer
  • Git installed on your computer
  • You have a billable Azure account
  • You have a Docker hub account
  • You have installed the Azure CLI on your computer
Let us start with a very simple Node-Express application. I have one that you can clone from GitHub. Run the following command from a working directory in order to clone my simple Node-Express application:
Once you have cloned the application, you can create a Docker image named toon-node. View a file named Dockerfile located in the root of the application. This file looks like this:
# Create image based on the official Node 6 image from the dockerhub
FROM node:10.13.0-jessie

# Create a directory where our app will be placed
RUN mkdir -p /usr/src/app

# Change directory so that our commands run inside this new directory
WORKDIR /usr/src/app

# Copy dependency definitions
COPY package.json /usr/src/app

# Install dependecies
RUN npm install

# Get all the code needed to run the app
COPY . /usr/src/app

# Expose the port the app runs in
ENV PORT 80

# Serve the app
CMD ["npm", "start"]
This is what each command in Dockerfile does.
FROM node:10.13.0-jessie Base the new image on node:10:13.0-jessie
RUN mkdir -p /usr/src/app Create a directory /usr/src/app in the container
WORKDIR /usr/src/app Make /usr/src/app the working dir in the container
COPY package.json /usr/src/app Copy package.json on your computer into the working directory in the container
RUN npm install Execute "npm install" in the container
COPY . /usr/src/app Copy all files in the. current directory of your computer into the container’s working directory. Note that any files in .dockerignore will be ignored
ENV PORT 80 The node application inside the container will listen in on port 80
CMD ["npm", "start"] Launch the web application by executing “npm start”.
Create a Docker image with the following terminal command:
docker build -f Dockerfile -t snoopy/toons:toon-node . --no-cache
Note that you must prefix the image name with your Docker hub account. In the above example, snoopy represents your username on Docker hub.
Once the image is created, you can verify that it indeed exists by using the following Docker terminal command that lists on the images on your computer:
docker images
You should see an image named toons:toon-node prefixed by your Docker hub username.
Now let us run this Node-Express application to see what it does. We will create a container from the image that we just created. Run the following command to create a container whereby port 80 in the container is mapped to port 3000 on your computer:
docker run -p 3000:80 snoopy/toons:toon-node
You can then point your browser to http://localhost:3000 to see what the node-express application looks like. If all goes well, you should see the following web page:
 
The next thing we need to do is push our image to Docker hub. This is done by first logging-in into Docker hub with:
docker login
Note that this needs to be done only once on the computer.
You can then push the image to Docker hub with:
docker push snoopy/toons:toon-node
If you go to Docker hub at https://hub.docker.com/, you will see that the image exists in the toons repository. This is what my toons repository looked like on Docker hub:
With our image sitting in the Docker hub repository, we can now start using Azure to pull the image and run a container in Azure.
Login into Azure on your computer using the Azure CLI (command line interface). This is done by running the following terminal window command:
az login
This opens a browser window in which you will be authenticated. You can close the browser once you have successfully authenticated. 
We need to either use an existing resource group or. create a new one. I created a new resource group named toon-rg in data centre West-US2 with the following terminal window command:
az group create --location westus2 --name toon-rg 
Once you execute the above command, a successful response from the server will look similar to this:
{
  "id": "/subscriptions/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee/resourceGroups/toon-rg",
  "location": "westus2",
  "managedBy": null,
  "name": "toon-rg",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": null
}

Let us use the image we have in Docker hub to run a container in Azure. This can be done by executing the following command:
az container create --name toon-c --resource-group toon-rg --image snoopy/toons:toon-node --ip-address public
These are what the above settings represent:
--name toon-c The container name is “toon-c".
--resource-group toon-rg The resource group name is “toon-rg".
--image snoopy/toons:toon-node The name of our image on Docker hub is "snoopy/toons:toon-node".
--ip-address public Respond with the public IP address so that we can run and verify that indeed our web site is working on Azure.
Remember that you must replace snoopy with your Docker hub username.
When I ran the last command, it took some time to run and I eventually got a long JSON response. I am showing you a part of the response that has the public IP address:
You can see the public IP address in the JSON response. If you point your browser to this IP address you should see the application running on Azure. This is what I experienced when I pointed my browser to http://52.247.209.239
It is rather impressive how easy it is to host a Docker container on Azure.

Saturday, February 2, 2019

Deploying Node Express app to Azure through Azure DevOps

In this tutorial, we will deploy a simple Node.js application that displays the responses from two separate APIs. First the application's source code is pushed to Azure DevOps using Git. The CI & CD pipelines are setup inside Azure DevOps. The application is eventually deployed to Microsoft Azure App Services.

The following is assumed:
  • You have node.js & git installed on your computer
  • You have an Azure DevOps account. Otherwise, create one at http://dev.azure.com.
  • You have a Microsoft Azure account.
Preparation
Clone a sample application from GitHub with the following terminal command:

git clone https://github.com/medhatelmasry/json-express

Create a new project in Azure DevOps. I created one called Toons.

Click on Repos on the left-side menu. Copy the URL under “Clone your computer” into the clipboard.

Go into a terminal window at the root of your application and execute the following Git commands:

git init
git add .
git commit -m "First commit"
git remote add azdev {paste the git URL here}
git push azdev master

Upon refreshing the Azure DevOps page in your browser, you should see your source code.
 

Build

Let us create a build. Click on Pipeline >> Build on the left side in Azure DevOps. Click on “New pipeline” in the middle of the page:

On the next page, choose “Azure Repos Git” then click on Continue.

Select the “Node.js With Gulp” template.
 
This creates for you these set of tasks:
 
We want to enable “Continuous Integration” so that whenever new code is pushed into the Git repo it automatically triggers a build. To do this, click on Triggers at the top. Check the “Enable continuous integration” checkbox.
 
To start the build, click on “Save & queue” >> “Save & Queue”. Ignore all the other input fields and click the “Save & queue” button on the next page.
 
You can see build progress by clicking on the build # on the top.
 
If things go well, all tasks will complete successfully.

The next step is to create a web app in Azure. In your browser, go to http://portal.azure.com and create a web app. Click on App Services on the left-side, then click on the “Add" button.
 
From the available templates, choose “Node JS Empty Web App”.
 
Click Create to go the next page.
You will be asked for a unique host name. Accept the other defaults then click on the blue Create button. It will take about 2 minutes to provision your website. Once the website has been provisioned, return to Azure DevOps in your browser.

Release

The next step is to create a release in Azure DevOps. Click on Pipelines >> Releases on the left side. Click on the blue “New pipeline” button in the middle of the page.

On the next page, choose the “Deploy a Node.js app to Azure App Service” template, then click on the blue Apply button.
 
In the left-side box, click on “Add an artifact”. A dialog pops up on the right-side. From the Source drop-down-list, choose a  build then click on the blue Add button.

Let us add continuous deployment. Click on the thunderbolt symbol in the top-right of the first box.
 
Enable the “Continuous development trigger”.

Next, we need to setup the connection between DevOps and Azure. Click on “1 job, 1 task” in the second box.

Select the correct subscription under Azure subscription. While authorizing you may need to allow popups in your browser from the Azure site.
Under “App service name” select the web app that you created in Azure.
 
Once all these configurations are OK, click on the Save button on the top. After the release definition is saved, the Release button lights up. Click on “Release” >> “Create a Release”.
Choose a Stage and build artifact then click on Create.

Click on the release link on the top.

On the next page, click on the second “Stage 1” box.

Click Deploy on the following page.

On the dialog that pops up on the right-side, click on the blue Deploy button.

You should see progress of the release process. Copying of the contents of node_modules takes the longest time.

Once it completes successfully, you can request the web app in your browser.
  

Saturday, January 26, 2019

Node Express with TypeScript

Introduction
This walkthrough is intended to help you convert a standard Node Express application so that it uses TypeScript instead.

Preparation
Install TypeScript globally:

npm install typescript -g

Let us create a directory simply to play around and understand how TypeScript works:

mkdir tsprimer
cd tsprimer
code .

Create a file named greeter.ts and add to it the following code:

function greeter(person) {
    return "Hello, " + person;
}
let user = "Jane Bond";
console.log(greeter(user));

To trandspile the app, type:

tsc greeter.ts

This transpiles the app to greeter.js, which gets created in the same directory. Run the application with:

node greeter.js

Interfaces
Let us modify greeter.ts so that it uses strong type checking. Create a file named person.ts and add to it the following Person interface:

export interface Person {
    firstName: string;
    lastName: string;
}

Add a file named greeterPerson.ts that uses the Person interface with the following code:

import {Person} from './person';
function greeter(person: Person) : string{
    return "Hello, " + person.firstName + " " + person.lastName;
}
let person : Person = { firstName: "Jane", lastName: "Bond" };
console.log(greeter(person));

Transpile the app with tsc greeterPerson.ts and run it with:

node greeterPerson.js

Classes
Create a file named student.ts to hold a Student class. The code is:

export class Student {
    fullName: string;
    constructor(public firstName: string, public middleInitial: string, public lastName: string) {
        this.fullName = firstName + " " + middleInitial + " " + lastName;
    }
    public getFullName = () : string => {
        return this.fullName;
    };
}

Notice the instance variable fullName, constructor and method getFullName(). Create a greeter file named greeterStudent.ts that uses the Student class as shown below:

import {Student} from './student';
let student  = new Student("Jane","M.","Bond" );
console.log(student.getFullName());

Transpile & run the application.

TS with Node Express
Go to the parent directory with:

cd ..

create a new directory named tsexpress and go into that directory:

mkdir tsexpress
cd tsexpress

Let us initialize a node project in the current directory with:

npm init

Let’s now install the typescript package locally with:

npm install typescript --save-dev

Open the current directory in Visual Studio Code.

Inside our package.json, add a script called tsc:

"scripts": {
    "tsc": "tsc"
},

This modification allows us to call TypeScript functions from the command line in the project’s folder.

Run the following command to initialize the typescript project by creating the tsconfig.json file.

npm run tsc -- --init

Edit tsconfig.json, uncomment outDir setting so that it looks like this:

"outDir": "./build",

Also, make sure you have these additional settings in tsconfig.json:

    "target": "es5", 
    "module": "commonjs",
    "sourceMap": true, 
    "strict": true,
    "pretty": true,
    "moduleResolution": "node",
    "baseUrl": "./app",
    "esModuleInterop": true

Add this to the bottom of tsconfig.json:

  "include": [
    "app/**/*.ts"
  ],
  "exclude": [
      "node_modules"
  ]

Installing express.js

Run the following command:

npm install express --save

Express and Typescript packages are independent. The consequence of this is that Typescript does not “know” types of Express classes. Run this specific npm package for the Typescript to recognize the Express class types.

npm install @types/express --save

Let us add a very simple Hello World Node Express application file. Create a folder named app and inside the app directory create a file named app.ts with this content:

// app/app.ts
import express = require('express');

// Create a new express application instance
const app: express.Application = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

To transpile your first TypeScript Node Express app, run the following command in the root directory of your application:

npm run tsc

This command automatically created the build folder and adds to it the transpiled app.js file.

 

 To run the application in build/app.js, type:

node build/app.js

Point your browser to http://localhost:3000. You should see the following:


Running TypeScript without transpiling
You can run TypeScript code directly with the ts-node package.

This package is recommended for development purposes only. To make the final deployment to production, always use the JavaScript version of your project.

The ts-node is already included as a dependency on another package, ts-node-dev. After installing ts-node-dev we can run commands that restarts the server whenever a project file changes. Install  ts-node-dev as follows:

npm install ts-node-dev --save-dev

Add the following two script lines into the package.json file:

"dev": "ts-node-dev --respawn --transpileOnly ./app/app.ts",
"prod": "tsc && node ./build/app.js"

To start the development environment:

npm run dev

 To run the server in production mode:

npm run prod

Pure TypeScript Classes
So far in our Node/Express sample application, we are not really using proper TypeScript syntax. Let’s start making use of the powerful TypeScript platform.

Create a file named server.ts in the app folder and add to it the following code:

// app/server.ts
import app from "./app";
const PORT = 3000;

app.listen(PORT, () => {
    console.log('Express server listening on port ' + PORT);
})

In package.json:

change app.js                 TO              server.js
change app.ts                 TO             server.ts

Create a folder named routes under app and add to it the following index.ts file:

// /app/routes/index.ts
import {Request, Response} from "express";
export class Routes { 
    public routes(app: any): void { 
        app.route('/')
            .get((req: Request, res: Response) => {            
                res.status(200).send('Hello World!');
        });    
    }
}

Replace code in app/app.ts with following code:

// app/app.ts
import express from "express";
import bodyParser from "body-parser";
import { Routes } from "./routes";

class App {
    public app: express.Application;
    public routePrv: Routes = new Routes();

    constructor() {
        this.app = express();
        this.config(); 
        this.routePrv.routes(this.app); 
    }

    private config(): void { 
        this.app.use(function(req, res, next) {
            res.header("Access-Control-Allow-Origin", "*");         
            res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");         
            res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
            next();
        });           

        // support application/json type post data
        this.app.use(bodyParser.json());
        //support application/x-www-form-urlencoded post data
        this.app.use(bodyParser.urlencoded({ extended: false }));
    }
}
export default new App().app;

Test the application in dev mode by running:

npm run dev 

Then point your browser to http://localhost:3000. You should see Hello World in the browser.

Test same as above in production mode by running:

npm run prod

Using mongoose

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. In this section we will talk get our application to conntect with a MopngoDB database using the Mongoose driver. It is assumed that you have a MongoDB database running on your development computer and listening on port 27017.

Install these mongoose packages:

npm install mongoose –save
npm install @types/mongoose --save

Create a folder named models under app and add to it a file named student.ts with the following code:

//   /app/models/student.ts
import mongoose from 'mongoose'; 
const Schema = mongoose.Schema;
// create a schema
export const StudentSchema = new Schema({
    FirstName: {
        type: String,
        required: true
    },
    LastName: {
        type: String,
        required: true
    },
    School: {
        type: String,
        required: true
    },
    StartDate: {
        type: Date,
        required: true
    }
});

Create a folder called controllers under app and add to it a file named studentController.ts with this code:

//   /app/controllers/studentController.ts
import * as mongoose from 'mongoose';
import { StudentSchema } from '../models/student';
import { Request, Response } from 'express';

const StudentMongooseModel = mongoose.model('Student', StudentSchema);

export class StudentController { 

    public addNewStudent (req: Request, res: Response) {                
        let newStudent = new StudentMongooseModel(req.body);

        /*
        let newStudent = new StudentMongooseModel({
            "FirstName": "Tom",
            "LastName": "Baker",
            "School": "45 River Road",
            "StartDate": "2001-11-17T19:23:15.118Z"
        });
        */
    
       newStudent.save((err, data) => {
            if (err){
                res.send(err);
            }    
            res.json(data);
        });
    }

    public getStudents (req: Request, res: Response) {           
        StudentMongooseModel.find({}, (err, data) => {
            if (err){
                res.send(err);
            }
            res.json(data);
        });
    }

    public getStudentById (req: Request, res: Response) {           
        StudentMongooseModel.findById(req.params.studentId, (err, data) => {
            if (err){
                res.send(err);
            }
            res.json(data);
        });
    }

    public updateStudent (req: Request, res: Response) {           
        StudentMongooseModel.findOneAndUpdate({ _id: req.params.studentId }, req.body, { new: true }, 
            (err, data) => {
            if (err){
                res.send(err);
            }
            res.json(data);
        });
    }

    public deleteStudent (req: Request, res: Response) {           
        StudentMongooseModel.findOneAndRemove({ _id: req.params.studentId }, (err, data) => {
            if (err){
                res.send(err);
            }
            res.json({ message: 'Successfully deleted student!'});
        });
    }

    public generateDummyData (req: Request, res: Response) {     
        var data = [
            {
            "FirstName":"Sally",
            "LastName":"Baker",
            "School":"Mining",
            "StartDate": new Date("2012-02-20T08:30:00")
            },{
            "FirstName":"Jason",
            "LastName":"Plumber",
            "School":"Engineering",
            "StartDate": new Date("2018-03-17T17:32:00")
            },{
            "FirstName":"Sue",
            "LastName":"Gardner",
            "School":"Political Science",
            "StartDate": new Date("2014-06-20T08:30:00")
        },{
            "FirstName":"Linda",
            "LastName":"Farmer",
            "School":"Agriculture",
            "StartDate": new Date("2014-06-20T08:30:00")
            },{
            "FirstName":"Fred",
            "LastName":"Fisher",
            "School":"Environmental Sciences",
            "StartDate": new Date("2017-10-16T17:32:00")
            }
        ];
          
        StudentMongooseModel.collection.insert(data, function (err, docs) { 
            if (err){
                res.send(err);
            }
            res.json({ message: 'Successfully generated 5 sample documents!'});
        });
    }
}

Add this instance variable to the Routes class in routes/index.ts and resolve it by importing the appropriate module:

studentController: StudentController = new StudentController();

Add these routes to routes/index.ts:

        // Get all students
        app.route('/api/students')
            .get(this.studentController.getStudents);

        // Create a new student
        app.route('/api/students')
            .post(this.studentController.addNewStudent);

        // get a specific student
        app.route('/api/students/:studentId')
            .get(this.studentController.getStudentById);

        // update a specific student
        app.route('/api/students/:studentId')
            .put(this.studentController.updateStudent);
        
        // delete a specific student
        app.route('/api/students/:studentId')
            .delete(this.studentController.deleteStudent);
        
        // generate dummy data
        app.route('/api/dummy')
            .get(this.studentController.generateDummyData);

Notice that the last route, /api/dummy, which is for generating dummy data.
Back in /app/app.ts, import mongoose with:

import mongoose from "mongoose";

Add this method to /app/app.ts:

    private mongoSetup(): void{
        mongoose.connect('mongodb://localhost:27017/school', {})
        .then(() => console.log('connection successful'))
        .catch((err) => console.error(err));
    }

In the constructor of /app/app.ts, call the above method with:

this.mongoSetup();

Start the server with:

npm run dev

In your browser, point to http://localhost:3000. You should see Hello World.

Next, point your browser to http://localhost:3000/api/dummy. This should create sample data.

Use URL http://localhost:3000/api/students to see the sample data that was jiust created.