March 8, 2022

Node.js – Create a Node.js API Using TypeScript

In this tutorial, you will learn how to setup a Node.js API using TypeScript. In the previous tutorials, we created an API using Node.js and Javascript. You can find them below:

We would follow the steps to first setup Node.js application, then TypeScript and finally build an API. We would do the following:

  1. Do Some Initialization
  2. Initialize and Configure tsconfig.json File
  3. Modify the Package.json File
  4. Setup the Application Structure

 

1. Do Some Initialization

Step 1 – Create a folder and open this folder using IntelliJ

Step 2 – Initialize Node.js using the command npm init and then npm install.

Step 3 – Then you need to install Typescript using globally

This provides the typescript compiler which makes it possible to compile Typescript code into JavaScript

Step 4 –  Install the following dependencies

  • ts-node
  • express
  • @types/express
  • nodemon

 

2. Initialize and Configure tsconfig.json

Step 1 – Initialize TypeScript using the command tsc –init

This would create the tsconfig.json file.

Step 2 – Open the tsconfig.json file and ensure the following settings are enabled

"compilerOptions": {
    "forceConsistentCasingInFileNames": true,
        "module": "commonjs",
        "esModuleInterop": true,
        "outDir": "./build",
        "rootDir": "./src",
        "target": "es6",
        "skipLibCheck": true,
        "strict": true
}

 

3. Modify the package.json File

We would now modify the package.json file to do two things:

  • to specify the startup file
  • allow for automatic server restart once something changes
"main": "source/server.ts",
"scripts": {
    "dev": "nodemon source/server.ts",
    "build": "rm -rf build/ && prettier --write source/ && tsc"
}

With the above code, the typescript files would be build and compiled into JavaScript file.

Then we can start the server using the command npm run dev

 

4. Setup the Application Structure

We would have to create an src folder that would hold our application files. Then inside the src folder, we would have 3 folders: services, controller and routes

Step 1 – Create the src folder

Step 2 – Inside the src folder, create 3 folders: services, controllers and routes

Step 3 – Inside the controllers folder, create a file named home-controller.ts. This would be the content

import {Request, Response} from 'express';
import * as service from '../services/home-service'

const getHome = async (req: Request, res: Response) => {
    return res.send(service.goHome());
};

export default {getHome}

 

Step 4 – Inside the services folder, create a file named home-service.ts. This would be the content

export function goHome() {
    return "Kindson is here now yesss"!
}

 

Step 5 – Inside the routes folder, create a file named app-routes.ts, this would be the content

import express from 'express'
import controller from '../controllers/home-controller'

const router = express.Router();

router.get('/home', controller.getHome);

export = router;

 

At this point we have setup the API with TypeScript and now you can run the application using the command npm run dev.

Then you can access the /home url and see that you get a response!

In the next part, we would then write the services that retrieves data from PostgreSQL database.

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments