Introduction to TypeScript and Node.js

Introduction to TypeScript and Node.js#


Chrome and the V8 JavaScript engine that power it were released in late 2008. Soon after, Ryan Dahl from Google had the idea of using V8 to run JavaScript on the server. This idea became Node.js and was eventually released in 2009. After a few years, large companies the likes of Airbnb and Uber had adopted Node.js. Today, Node.js is the web's most popular framework

Using the same language on the server and client makes it easier to share code, and it allows JS developers to program both the front-end and back-end.

Using TypeScript on the server brings with it all of the benefits of using TypeScript on the client, and it opens the door for sharing types across the stack, so there won't be a mismatch between server and client types. We'll go over an example of sharing types across the client and server in the Express Server section of this chapter. For a comprehensive argument on why you should use TypeScript in general see the opening section of Chapter 1.

This chapter is split up into three sections:

  • Working with the File System: Node has a standard library for interacting with the file system. In this section we'll see how you can add some type safety when reading and writing files.

  • Working with HTTP: Node has all of the functions you need to build a server from scratch. In this section we learn about the core APIs that power server frameworks the likes of Express and Koa.

  • Express Server: Express continues to be the framework of choice for building servers in Node. In this section we build a type-safe JSON API that shares code with a web application.

The sections are self-contained, so if you find a topic more intersting than the other, feel free to skip to that section.

Let's get started!

Working With The File System#

Having the ability to work with the file system is important because it allows us to read and write to files that persist on the machine. Examples include reading/writing JSON files, getting a list of files in a directory, and minifying images.

In this section we will be building our own utility library for interacting with JSON files. We'll call our library fs-json. Our library will allow us to easily read and write JSON files to the system using Promises.

Configuring Development Environment#

You'll need to have Node, TypeScript, and ts-node installed. Refer to the first chapter for installation instructions if you haven't already.

Our library will live in a folder called fs-json. Run the following command to create a folder with that name and set the current directory to the newly created folder:

Once you have Node and TypeScript installed and you've created the fs-json directory, you can install the dependencies that are specific to our project by running the following command:

The @types/node dependency contains the TypeScript definitions for Node.js. Without it, our TypeScript program wouldn't recognize the types of the standard library.

Building fs-json#

fs-json will expose the following API to allow users to read and write JSON files:

  • readFile(filePath) - returns a Promise that resolves to the JSON contents of the given filePath.

  • writeFile(filePath, contents) - returns a Promise that resolves when the file at the given path has been updated with the given contents.

We'll start by implementing the readFile() function in a file named readFile.ts. Let's start by mirroring the built-in fs.readFile() function:

The fs.readFile() function uses a node-style callback to communicate success or failure of reading the file, so the second parameter to our function also takes a node-style callback. To be able to test our function, let's create a file db.json with a simple JSON structure:

We'll also create an app.ts file where we can put our code testing the functions. We'll initialize the file with the contents below:

And we run the following command to run the code in app.ts:


This page is a preview of Beginners Guide to TypeScript

Please select a discussion on the left.