Book Image

Learning Node.js for .NET Developers

Book Image

Learning Node.js for .NET Developers

Overview of this book

Node.js is an open source, cross-platform runtime environment that allows you to use JavaScript to develop server-side web applications. This short guide will help you develop applications using JavaScript and Node.js, leverage your existing programming skills from .NET or Java, and make the most of these other platforms through understanding the Node.js programming model. You will learn how to build web applications and APIs in Node, discover packages in the Node.js ecosystem, test and deploy your Node.js code, and more. Finally, you will discover how to integrate Node.js and .NET code.
Table of Contents (21 chapters)
Learning Node.js for .NET Developers
About the Author
About the Reviewer


The purpose of this book is to help .NET or Java developers make the leap to Node.js. You may have some web development experience, and perhaps you've written some browser-based JavaScript in the past. It might not be obvious why anyone would want to take JavaScript out of the browser and use it for server-side development. However, this is exactly what Node.js does. What's more, Node.js has been around for long enough now to have matured as a platform, and has sustained its impressive growth in popularity well beyond any period that could be attributed to initial hype over a new technology.

The first objective of this book then is to explain why Node.js is a compelling technology that's worth learning more about. The first few chapters introduce Node.js with this in mind, quickly get you up and running with Node.js, and provide an important (re) introduction to the JavaScript language to set you on the right track.

The main part of this book will then take you through a worked example of building up a Node.js web-application step by step. In the process, we'll illustrate all the important tools and techniques required for real-world development projects in Node.js. The aim is to make the most of your existing development expertise to allow you to quickly reach the same level of best practices and professionalism with Node.js.

The final chapters of the book show how to use Node.js for other purposes outside of web applications and how to continue learning Node.js and exploring the ecosystem around it. We'll also see how you can use Node.js alongside .NET and benefit from applying your programming skills across both technologies.

What this book covers

Chapter 1, Why Node.js?, introduces Node.js as a programming platform. It covers the execution model of Node.js, particularly how it differs from .NET and Java, and the use cases in which these differences become strengths. This chapter also discusses the suitability of JavaScript as a development language.

Chapter 2, Getting Started with Node.js, dives straight into creating a Node.js application. In this chapter, you will install Node.js, choose a code editor, and set up a minimal web application project. You'll also learn some important command-line tools for working with Node.js.

Chapter 3, A JavaScript Primer, introduces the most important things to know when programming in JavaScript. It describes the JavaScript type system and its particular flavor of functional object-oriented programming, including prototype-based inheritance. This chapter also covers a few key gotchas and JavaScript language quirks.

Chapter 4, Introducing Node.js Modules, explains how to structure JavaScript applications using modules. It introduces the Node.js module system and shows you how to use this to organise your application's code.

Chapter 5, Creating Dynamic Websites, expands on the examples from the previous chapter to build a functioning web application. You'll add a JSON API and dynamic views to your application and communicate between the client and server using Ajax.

Chapter 6, Testing Node.js Applications, shows you how to write automated tests in JavaScript and Node.js. It introduces a number of tools and libraries for writing and running tests in JavaScript, and guides you through writing a variety of unit tests and integration tests for your application.

Chapter 7, Setting up an Automated Build, covers build automation and continuous integration in Node.js. You'll set up a CI server and task runner for your application, adding automated tasks to run tests, execute static analysis, and assess code coverage.

Chapter 8, Mastering Asynchronicity, introduces different patterns for asynchronous programming in JavaScript. You'll apply these to your own application and make the most of JavaScript language features and libraries for simplifying asynchronous code.

Chapter 9, Persisting Data, explains persistent data stores that can be used with Node.js. It introduces MongoDB and Redis, explaining their different data models and their use cases. You'll integrate both of these data stores with your Node.js application.

Chapter 10, Creating Real-time Web Apps, shows how to implement real-time two-way communication between the client and the server. You'll use the Socket.IO library to add real-time functionality into your application. You'll also see how to write tests for this functionality and how to write scalable real-time applications using Redis as a backend.

Chapter 11, Deploying Node.js Applications, demonstrates how to get a Node.js application onto the Web. You'll deploy your application to a free cloud-hosting provider. You'll see how to configure data stores and how to use remote server logs for debugging.

Chapter 12, Authentication in Node.js, covers authentication for Node.js web applications. You'll implement authentication using third-party providers, integrate this with your application, and show different content to logged-in and logged-out users.

Chapter 13, Creating JavaScript Packages, explains how to create standalone JavaScript packages for use by others. You'll see how to write universal JavaScript libraries that can run on both the client and the server, and how to write a standalone command-line application using Node.js.

Chapter 14, Node.js and Beyond, puts the content of this book in a wider context. It explains how Node.js and JavaScript are continuing to evolve, so you can be prepared for and take advantage of upcoming changes. It covers some alternative programming languages for Node.js and the Web, and how these relate to JavaScript. It discusses how some of the principles from Node.js can be applied to .NET programming, and illustrates how these are particularly visible in .NET Core (the new version of .NET). It also shows how you can use Node.js together with .NET to gain the best of both worlds.

What you need for this book

All of the tools and services used in this book are available for free online. Most of the worked examples require an active web connection at some point. To get started, you need nothing more than a console, a web browser, and permission to install new software on your machine. To support developers coming from a .NET background, some of the console listings or example steps in this book use Windows conventions (for example, backslashes in paths). None of the examples depend on Windows specifically though. You can work through this book on Windows, Mac OSX, or Linux.

Who this book is for

This book is for .NET or Java developers who are interested in learning Node.js. No prior experience with Node.js is expected. You might have written some client-side JavaScript before, but this is not required. The main worked example in this book is a Node.js web application. Web development experience in .NET or Java will be helpful, but it's not necessary to have experience with any particular application library or framework.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "ES2015 introduces the let keyword for declaring variables."

A block of code is set as follows:

<!DOCTYPE html>
    <title>{{ title }}</title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
    <h1>{{ title }}</h1>
    <p>Welcome to {{ title }}</p>

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express', name: 'World' });

Any command-line input or output is written as follows:

> npm install –g nodemon

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking the Next button moves you to the next screen."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from and also from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.