Create production ready REST API using nodeJS with TypeScript
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.
- 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.
- 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
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.
- Lazy Panda Tech
Views 1,190 & counting...