Create production ready REST API using nodeJS with TypeScript

In 2020 era, if a new application is written using NodeJS, then it is highly recommended to use typeScript in behind instead of a plain javaScript. TypeScript is more usefull and day by day many developers are accepting it to create an application. And there are a good numbers of developer community to solve your problems. So, if you have not tried it yet,do it now. 

 

It is true that I have search a lot for a proper production ready NodeJS with TypeScript code, but believe me I have struggled a lot and started building it by myself. Though, few of the github code I have downloaded and checkout how it works. I found one clean and fresh code (I forgot the link)and on top of it, I have added my idea to create a simple, production ready and extendable NodeJS with TypeScript code with Swagger enabled. 

Why TypeScript?

- From the name "Type", it defines the property that should have either a primitive type or a custom type. TypeScript provides a complete Object Oriented Programming concept and you can bring any kind of design pattern as well. It supports Class, interface, Exception, decorator etc which definetly makes your life easy. Even though it can run on any device like browser, device or any operating system, it is not specific to any virtual machine etc. and typeScript is compilable and outcome becoms a javascript. 

Even though it can run on any device like browser, device or any operating system, it is not specific to any virtual machine etc. and typeScript is compilable and outcome becoms a javascript. 

Difference between TypeScript and JavaScript:

TypeScript JavaScript
TypesScript is known as Object oriented programming language. JavaScript is a scripting language.
TypeScript has a feature known as Static typing. JavaScript does not have this feature.
TypeScript gives support for modules. JavaScript does not support modules.
TypeScript has Interface. JavaScript does not have Interface.
TypeScript support optional parameter function. JavaScript does not support optional parameter function.

 

Advantages:
  • It is supporting strongly, type or static typings that means, static typing allows for checking type correctness at compile time.
  • Typescript has a full flavour of Object Oriented Programming concept, so that your application design will be robust and extendable.
  • TypeScript is nothing but JavaScript and some additional features i.e. ES6 features. It may not be supported in your target browser but TypeScript compiler can compile the .ts files into ES3, ES4 and ES5 also.
  • It has a great support on VSCode.

Let's start building the application - 


Dependencies On NodeJS typeScript

  • @types/node - typed node js
  • dotenv, @type/dotenv - set up for environment variables
  • express  - server
  • typescript - typescript support
  • swagger-ui-express@types/swagger-ui-express - swagger UI for endpoint

 


Architecture Diagram

nodejs-typescript-arch-diagram

 

The sample I am going to create here, will follow the above diagram. If you need more abstraction layer, you can improvise the architecture as well. But, for the time being, I am making it simple. Here, you will see, there are decoupled handlers which are build to serve its purpose independently and individually. 


Demo Time and Code Debuggeing

 

 


Deploy the nodejs with typescript code to Heroku

To deploy the typescript anabled nodejs code to heroku, you need to follow few steps. 

  • Should not deploy the .ts file under src folder.
  • Build and get dist file.
  • Add dist file to your repository and link it from heroku.
  • modify the package.json file with your heoku command.
  • select proper branch and deploy the code.

 

You can find the complete code from here. Please share your suggestions to improve the article.

Happy Coding!

- Lazy Panda Tech