How to structure your TypeScript Node.js Application

How to structure your TypeScript Node.js Application

Some hands-on examples, and a couple of important basic architectural principles

For me, it’s always hard to start a new application from scratch.

It’s especially hard when coming from frameworks like Ruby on Rails or Symphony, where you get your initial application structure out of the box.

In the world of TypeScript, we tend to refrain from the idea of sometimes opinionated frameworks. It’s up to us to come up with something great from scratch and it usually takes several iterations to get it right. That’s why I decided to write a small practical guide from my personal experiences on how you can structure your TypeScript Node.js app.

This article will include some hands-on examples, and I will touch on a couple of important basic architectural principles. We are going to use a simplified version of the ports and adapters architecture.

It’s useful if you are already familiar with the following concepts:

  • You have a good understanding of Typescript.
  • You know basic OOP concepts such as interfaces and factory functions and should be familiar with polymorphism. We will apply those principles with a functional coding style.
  • You have a love for software architecture and would like to learn more about it.

I will first introduce the concepts and then go through a hands-on example of how you can apply those while designing your application.

Ports and Adapters

The idea of Ports and Adapters got quite popular over the past decade. It’s a simple but powerful idea on how you can structure your application:

The idea of Ports and Adapters is that the application is central to your system. All the inputs and outputs reach or leave the core of the application through a port. This port isolates the application from external technologies, tools and delivery mechanics. — __https://www.thinktocode.com/2018/07/19/ports-and-adapters-architecture/

Let’s look at a simple use case to understand what this means:

A user would like to print a document.

Let’s forget about the real complexity of the problem for a moment and only look at the essential parts: The first thing we need is a printing device that prints the document. We also need the driver that translates your virtual document into low-level and device-specific messages/commands that your printer can understand.

programming typescript javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

What is TypeScript? Why TypeScript? and Why Not TypeScript?

TypeScript extends JavaScript by adding Types. There are many great reasons to switch to TypeScript. Especially if your team uses JavaScript. There are some reasons to not use TypeScript as there are with any language or framework.

TypeScript Tutorial For JavaScript Developers - TypeScript Basics

TypeScript Tutorial For JavaScript Developers - TypeScript Basics. I will show you guys 4 example of JavaScript code, and how to convert it to TypeScript. This is a typescript beginners tutorial.

TypeScript: A Superset of JavaScript

Multi-paradigm, Functional, Generic, Imperative, Object-Oriented. In this article, I have shared the essential information about TypeScript programming language. This article contains a basic overview, brief history, features and applications of TypeScript. TypeScript is getting more popular in Full-stack development. In terms of popularity, in the last two years TypeScript pulled ahead of Ruby, Shell, and C.

How an Anti-TypeScript “JavaScript Developer” Like Me Became a TypeScript Fan

In this article, I will discuss my journey from being an anti-TypeScript developer to a developer who now couldn’t think of going back to the plain JavaScript world. Let's see How an Anti-TypeScript “JavaScript Developer” Like Me Became a TypeScript Fan.

Functional Programming: Using the Functor Type Class in TypeScript

Functional Programming: Exploring the Functor type class and its use cases