Taking JavaScript and .NET to the cloud

Menu

MEAN Stack

Introduction

After putting aside some time to play around with MeanJS, I wanted to share my thoughts on getting my environment set up for migrating the JavaScript code over to TypeScript.

Get MeanJS running locally

My first task was to make sure I had everything needed to run the MeanJS code as-is from GitHub. I was fortunate to have all the prerequisites installed, including node and MongoDB, but if any further information is needed, the readme provides plenty of help.

To get everything running, I simply ran the following commands in the project’s directory:

$ npm install
$ bower install
$ grunt

If everything went well, you should be greeted with the following page on localhost:3000:

Add type definitions

Now that we know the code works, let’s get started with mixing in some TypeScript. We’ll start by automating the retrieval of type definition files:

Well that didn’t go very well. My suspicion is that the compiler under Visual Studio is doing some strange things which I will need to investigate further. In the meantime, I’ll just use a gulp file similar to one discussed earlier to compile the project… and success!

Conclusion

So we hit a bit of a snag in getting the environment set up. Alongside with the compilation not working under Visual Studio, The IDE was very sluggish with the node project open. Although the node tools are still in beta, it is still quite disappointing considering that Visual Studio is my IDE of choice and would very much like to keep it that way. I will continue to work on this and hopefully come up with some more interesting points for discussion in the future.

Oh, and my first attempts will be up on GitHub as soon as I can make more substantial progress in my migration efforts.

Now that I have spent several weeks looking into TypeScript and the MEAN stack, I have decided to spend the next few weeks to attempt and create a more complete application stack beyond a simple “hello world” application.

After looking at the various MEAN solutions out there, I chose to use MEAN.JS as a basis for the project. This is a popular option with great documentation and a growing community.

My plan from here is:

To convert portions of the server-side code to TypeScript.

To convert portions of the client-side code to TypeScript.

To convert any remaining JavaScript code, such as tests, to TypeScript.

Over the following weeks, I will use this blog to track my progress, but also to share the challenges and topics of interest encountered. The code will be open source and available on GitHub.

This may end up as an interesting proof-of-concept boilerplate application, or as a catastophic mess. Nonetheless, I would not consider the outcome a failure, as the aim is to at gain some further insight in using TypeScript along with Node.js, Angular.js and Express.js. If others will find this journey interesting, then even better!

Introduction

Up until now, we set up the main tools necessary to get TypeScript and the MEAN stack up and running. We got a basic server application running against MongoDB. Today we will dive into the client side, getting a basic front-end written up using AngularJS.

Bower

Similar to npm, bower is a package manager that aids in obtaining packages for use on the front-end. Installation can be done via npm:

$ npm install –g bower

We will create two files to have bower working nicely in our project. The file .bowerrc will allow us to specify where the packages go, and bower.json works in a similar way to package.json, but for bower instead of npm:

Now that we have the packages installed, we need to grab the type definition files for angular, bootstrap and jQuery, since angular has a dependency on jQuery. This is the exact same process described in previous posts.

Updates on the server

Updates on User Controller

The controller on the server needs to be updated to return JSON instead of plain text. We also need a method to retrieve all the users:

Client code

From the client side, we will go ahead and create a page for users and a controller. Angular offers many more things, but again, we are keeping this simple for now, making sure we have basic connectivity and data retrieval out of the way. Later on we will refactor this code and follow established practices.

Let’s go ahead and create the necessary files and folders in the folder public:

The Controller

For the sake of brevity, we will create the User class, the controller and relevant interfaces in a single file in userClientController.ts:

Run the application

Once we run the application, we should be greeted with this simple view:

We can now enter a name and hit the “+” button. The user “angular” should appear on the list.

Let’s check to see what’s happening on the server. Having a look at some node output, we see a post made:

And finally, having a look at what’s in MongoDB:

Great! So we go a (very) basic application working!

Conclusion

Now we have successfully hooked up an application that has a Node/Express back-end, an Angular front-end, and a MongoDB data store. The application itself is very basic, having been used as an opportunity to dig into TypeScript, but is a decent starting point for further investigation. I will continue digging into these concepts, aiming to flesh out the core idea into a more fully-fledged stack for various projects.

Introduction

In my previous post, we got a brief idea on what TypeScript and the MEAN stack are. We installed the essential tools and put together a simple node/express application. Here, we will continue from where we left off, connecting the application to MongoDB.

Some Quick Housekeeping

Before we get started with hooking up the application with MongoDB, let’s make the following file and folder changes:

Create new folder called app at the root level of the project.

Move the routes and view folders to the app folder.

Rename app.ts to server.ts.

Rename index.ts and user.ts in the app/routes folder to indexRoutes.ts and userRoutes.ts respectively. Module dependencies should also be updated.

Create a new folder called controllers in the app folder and add a new TypeScript file userController.ts to it.

Create a new folder called models in the app folder and add a new TypeScript file userModel.ts to it.

The app folder should have the following structure:

Hooking up the application to MongoDB

We are going to create a simple entity and attempt to create and retrieve some instances, using MongoDB for persistence. There are quite a few ways to achieve this, but for the time-being, we are going to use mongoose.

Install mongoose

We can add mongoose via npm. After the package is retrieved, let’s get the mongoose definition file from the definitely typed repository and add it to Scripts/typings/mongoose.

There is a bit of duplication here. Mongoose requires the use of schema definition code for data validation, type casting, etc. In TypeScript, we also need to define interfaces that extend mongoose documents. This is not ideal, and it would obviously be better if we can get the two aspects working better together. This is a topic that would require further investigation. For the time being, this will have to work.

Create the controller

My aim is to keep the controller as the entry point of the web requests and to maintain whatever application logic is needed. The controller will take the request, process the data in some meaningful way via the repository, and depending on the result, provide a suitable response.

Some tests

Now we can run the application and fire up a REST client. Again, keeping things simple, let’s go ahead and add the URL below and setting the method to POST. Sending the request will give us a response that the user was created:

Now that we created a user, we can also retrieve with the same URL and with the GET method:

So far, so good. Let’s just check to see that we do have something stored in our database. Using Robomongo, we add our local connection, which should appear automatically in the connections dialog:

The database “mean-type-dev” should be found, along with the collection users. Looking at the users collection, we find a user, which is in fact the user we created:

So there you have it. We now have a node application that can play nicely with MongoDB, with everything written up in TypeScript.

Conclusion

Today we were able to hook up our application with MongoDB via mongoose and perform some basic database operations. My next post will focus more on the client side, digging into angular.

Introduction

Two areas that have gained my attention lately are TypeScript, Microsoft’s offering in making JavaScript more attractive for use in large-scale applications, along with the MEAN stack, a collection of open-source libraries that allows the use of JavaScript for web application development. I think it would be an interesting experiment to implement a basic MEAN stack in TypeScript instead of JavaScript.Today I will delve into what it takes to get the essentials working on a Windows development environment and create a simple project in Visual Studio.

Install the essentials

Prerequisites

There is a lot of information get the MEAN stack up and running on Linux and Mac environments and less information for Windows environments, even though the process is quite straightforward. At least for this tutorial, I assume you have a machine with some version of Windows installed, along with your favourite IDE. I have Windows 7 (Not yet ready/willing to jump to 8 yet) along with Visual Studio 2013. My plan is to start with the Node.js tools for Visual Studio (More on that below) and then move on to another IDE such as Sublime Text.

Git

Git will be used for source control. Git’s popularity can’t be ignored, and I hope to release a reasonable take sometime in the near future. Git for Windows provides all the necessary tools to get Git running on Windows, along with a nice bash shell. Despite being a sucker for Windows, I’ve always enjoyed the Unix-style commands over the command prompt.

MongoDB

MongoDB provides the data storage component of the MEAN stack. More specifically, it is a document-oriented NoSQL database. Along with the benefits of availability and scalability that we get from most NoSQL databases, all data is stored in JSON-like documents, providing a nice fit with JavaScript.

Once MongoDB is installed, we can run MongoDB as a Windows Service (Make sure you run as administrator):

Node.js

Node.js is a platform that allows the development of scalable server-side applications with JavaScript. In our case, node will run the compiled TypeScript code on the server. node also comes with npm, which provides a vast collection of modules that expand on what node can provide.

Packages can be installed locally to a specific project:

$ npm install <package>

Packages can also be installed globally:

$ npm install -g <package>

TypeScript

For this tutorial, Visual Studio 2013 will be used to create and compile the project. TypeScript is available with Visual Studio 2013 Update 2. In the future, command-line tools will be used, so it’s worth installing now:

$ npm install –g typescript

Node Tools

Node.js Tools for Visual Studio is still in beta, but it provides an excellent starting point for putting together a node project, especially if you are familiar with using Visual Studio.

Create a Project

Once everything is installed, we’ll create a Basic Express Application in Visual Studio:

Upon generating the application, we get the following project:

If you’re coming from a pure .NET world, there’s some new things for you:

The views generated are not in html, but in Jade. Jade files will be discussed in a future post.

Style-sheets is not written in CSS, but in a pre-processor language called Stylus.

The file package.json contains information on the npm packages the project depends on.

So a few things different from a standard node/express project. Obviously, there are no JavaScript files, with TypeScript files taking their place. Another point of interest are the typing files. For each of the installed npm packages, there is a type definition file, which allows the use of the npm packages with stronger type definitions in TypeScript. A vast collection of type definitions for several popular packages can be found at the Definitely Typed website. Seeing that we are there. I grabbed the latest versions of the definition files that I will be using in the project.

I see Express

If you look at the npm packages, you can see that Express is already installed as a dependency. Express sits on top of node and provides a framework to run node as a more complete web application, including the use of views, routing, etc.

I don’t see Angular

Angular is not an npm package, but it’s a framework that will be used client-side. Since the focus has been on server-side, Angular was not discussed. Angular will get its due in a future post.

Run the application

Once the npm packages are downloaded for the project we can hit F5 and see what happens. the TypeScript files are compiled into JavaScript and we get the following in a browser:

Success! We have a decent starting point, but we have quite a bit ahead. This is a good a place to take a breather.

Conclusion

Today we got all the necessary tools to get a simple node/express application written in TypeScript running in a Windows environment, along with having git and MongoDB installed. In part 2, I will attempt to hook up my application with MongoDB and in part 3, get angular working on the client-side.