DEV Community

Tariq Ali
Tariq Ali

Posted on

Creating a Node.js Server in TypeScript: A Step-by-Step Guide

Introduction

Node.js has become the go-to platform for building scalable and high-performance server-side applications, and TypeScript, with its strong typing and modern features, is an excellent choice for writing clean and maintainable code. In this article, we will walk you through the process of creating a Node.js server in TypeScript, covering everything from setting up your development environment to handling HTTP requests, with automatic compilation using a package.json script.

Code GitHub Link

Creating a Node.js Server in TypeScript: A Step-by-Step Guide with Automatic Compilation

Introduction

Node.js has become the go-to platform for building scalable and high-performance server-side applications, and TypeScript, with its strong typing and modern features, is an excellent choice for writing clean and maintainable code. In this guide, we will walk you through the process of creating a Node.js server in TypeScript, covering everything from setting up your development environment to handling HTTP requests, with automatic compilation using a package.json script.

Prerequisites

Before we dive into creating our Node.js server in TypeScript, make sure you have the following prerequisites installed on your system:

  • Node.js: You'll need Node.js to run JavaScript on the server. You can download it from the official website here and install it.

  • npm: npm (Node Package Manager) comes bundled with Node.js and is used to manage packages and dependencies.

  • TypeScript: Install TypeScript globally using…

Prerequisites

Before we dive into creating our Node.js server in TypeScript, make sure you have the following prerequisites installed on your system:

  • Node.js: You'll need Node.js to run JavaScript on the server. You can download it from the official website (https://nodejs.org/) and install it.
  • npm: npm (Node Package Manager) comes bundled with Node.js and is used to manage packages and dependencies.
  • TypeScript: Install TypeScript globally using npm with the following command:
npm install -g typescript
Enter fullscreen mode Exit fullscreen mode
  • Code Editor: Choose a code editor or integrated development environment (IDE) of your choice. Popular options include Visual Studio Code, WebStorm, and Atom.

Getting Started

Now that you have the necessary tools installed, let's start building our Node.js server in TypeScript.

  • Create a Project Directory: Begin by creating a new directory for your project and navigate to it using your terminal.
mkdir nodejs-typescript-server
cd nodejs-typescript-server
Enter fullscreen mode Exit fullscreen mode
  • Initialize a TypeScript Project: Run the following command to initialize a new TypeScript project. This will create a tsconfig.json file with default settings.
tsc --init
Enter fullscreen mode Exit fullscreen mode
  • Install Dependencies: To create a basic server, we'll need to install the express framework and @types/express for TypeScript type definitions.
npm install express @types/express
Enter fullscreen mode Exit fullscreen mode
  • Create a package.json File: If you haven't already, create a package.json file in your project directory by running the following command and answering the prompts:
npm init
Enter fullscreen mode Exit fullscreen mode

This will generate a package.json file with default values.

  • Add Compilation Script: Open your package.json file and add a "start" script that automatically compiles TypeScript to JavaScript and runs your server. Modify the "scripts" section like this:
"scripts": {
  "start": "tsc && node app.js"
},
Enter fullscreen mode Exit fullscreen mode

This script first compiles TypeScript with tsc and then runs the resulting JavaScript file with node.

Creating the Server

With the project structure in place and dependencies installed, let's create our Node.js server in TypeScript.

  • Create an app.ts file in your project directory.

import express from 'express';

const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello, Node.js Server in TypeScript!');
});

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});
Enter fullscreen mode Exit fullscreen mode
  • Start Your Server: Start your Node.js server by running the following command:
npm start
Enter fullscreen mode Exit fullscreen mode

Your server should now be running at http://localhost:3000, and you can access it in your web browser.

Conclusion

In this article, we've covered the essential steps to create a Node.js server in TypeScript with automatic compilation using a package.json script. You learned how to set up your development environment, create a basic server using the express framework, and automate the TypeScript compilation and server startup. With this foundation, you can start building more complex server applications with Node.js and TypeScript, taking advantage of TypeScript's strong typing and modern language features to write clean and maintainable code. Happy coding!

Top comments (0)