{"id":256,"date":"2022-03-08T11:30:18","date_gmt":"2022-03-08T11:30:18","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/nodejs\/?p=256"},"modified":"2022-04-09T18:02:53","modified_gmt":"2022-04-09T18:02:53","slug":"node-js-create-a-node-js-api-using-typescript","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/nodejs\/node-js-create-a-node-js-api-using-typescript\/","title":{"rendered":"Node.js &#8211; Create a Node.js API Using TypeScript (Part 1 &#8211; Setup)"},"content":{"rendered":"<p>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:<\/p>\n<ul>\n<li><a href=\"https:\/\/kindsonthegenius.com\/nodejs\/rest-api\/\" target=\"_blank\" rel=\"noopener\">Build and API in Node.js<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/nodejs\/node-js-express-framework\/\" target=\"_blank\" rel=\"noopener\">GET, POST, PUT and DELETE with Node.js<\/a><\/li>\n<\/ul>\n<p>We would follow the steps to first setup Node.js application, then TypeScript and finally build an API. We would do the following:<\/p>\n<ol>\n<li><a href=\"#t1\">Do Some Initialization<\/a><\/li>\n<li><a href=\"#t2\">Initialize and Configure tsconfig.json File<\/a><\/li>\n<li><a href=\"#t3\">Modify the Package.json File<\/a><\/li>\n<li><a href=\"#t4\">Create a Server<\/a><\/li>\n<li><a href=\"#t5\">Setup the Application Structure<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Do Some Initialization<\/strong><\/h4>\n<p><strong>Step 1<\/strong> &#8211; Create a folder and open this folder using IntelliJ<\/p>\n<p><strong>Step 2<\/strong> &#8211; Initialize Node.js using the command <strong>npm init<\/strong> and then<strong> npm install<\/strong>.<\/p>\n<p><strong>Step 3<\/strong> &#8211; Then you need to install Typescript using globally<\/p>\n<p>This provides the typescript compiler which makes it possible to compile Typescript code into JavaScript<\/p>\n<p><strong>Step 4<\/strong> &#8211;\u00a0 Install the following dependencies<\/p>\n<ul>\n<li>ts-node<\/li>\n<li>express<\/li>\n<li>@types\/express<\/li>\n<li>nodemon<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Initialize and Configure tsconfig.json<\/strong><\/h4>\n<p><strong>Step 1<\/strong>\u00a0&#8211; Initialize TypeScript using the command<em><strong> tsc &#8211;init<\/strong><\/em><\/p>\n<p>This would create the tsconfig.json file.<\/p>\n<p><strong>Step 2<\/strong> &#8211; Open the tsconfig.json file and ensure the following settings are enabled<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"background-color: #fff0f0;\">\"compilerOptions\"<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">:<\/span> {\r\n    <span style=\"color: #007700;\">\"forceConsistentCasingInFileNames\"<\/span>: <span style=\"color: #008800; font-weight: bold;\">true<\/span>,\r\n        <span style=\"color: #007700;\">\"module\"<\/span>: <span style=\"background-color: #fff0f0;\">\"commonjs\"<\/span>,\r\n        <span style=\"color: #007700;\">\"esModuleInterop\"<\/span>: <span style=\"color: #008800; font-weight: bold;\">true<\/span>,\r\n        <span style=\"color: #007700;\">\"outDir\"<\/span>: <span style=\"background-color: #fff0f0;\">\".\/build\"<\/span>,\r\n        <span style=\"color: #007700;\">\"rootDir\"<\/span>: <span style=\"background-color: #fff0f0;\">\".\/src\"<\/span>,\r\n        <span style=\"color: #007700;\">\"target\"<\/span>: <span style=\"background-color: #fff0f0;\">\"es6\"<\/span>,\r\n        <span style=\"color: #007700;\">\"skipLibCheck\"<\/span>: <span style=\"color: #008800; font-weight: bold;\">true<\/span>,\r\n        <span style=\"color: #007700;\">\"strict\"<\/span>: <span style=\"color: #008800; font-weight: bold;\">true<\/span>\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Modify the package.json File<\/strong><\/h4>\n<p>We would now modify the package.json file to do two things:<\/p>\n<ul>\n<li>to specify the startup file<\/li>\n<li>allow for automatic server restart once something changes<\/li>\n<\/ul>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"background-color: #fff0f0;\">\"main\"<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">:<\/span> <span style=\"background-color: #fff0f0;\">\"source\/server.ts\"<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">,<\/span>\r\n<span style=\"background-color: #fff0f0;\">\"scripts\"<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">:<\/span> {\r\n    <span style=\"color: #007700;\">\"dev\"<\/span>: <span style=\"background-color: #fff0f0;\">\"nodemon source\/server.ts\"<\/span>,\r\n    <span style=\"color: #007700;\">\"build\"<\/span>: <span style=\"background-color: #fff0f0;\">\"rm -rf build\/ &amp;&amp; prettier --write source\/ &amp;&amp; tsc\"<\/span>\r\n}\r\n<\/pre>\n<p>With the above code, the typescript files would be build and compiled into JavaScript file.<\/p>\n<p>Then we can start the server using the command npm run dev<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Create the Server<\/strong><\/h4>\n<p>We would have to create a server that startups up when we issue the command npm run dev.<\/p>\n<p><strong>Step 1<\/strong> &#8211; Create a file named server.ts outside the src directory. The content of the file is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> http from <span style=\"background-color: #fff0f0;\">'http'<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> express, {Express} from <span style=\"background-color: #fff0f0;\">'express'<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> routes from <span style=\"background-color: #fff0f0;\">'..\/src\/routes\/app-routes'<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">const<\/span> router: <span style=\"color: #333399; font-weight: bold;\">Express<\/span> <span style=\"color: #333333;\">=<\/span> express();\r\nrouter.use(<span style=\"background-color: #fff0f0;\">'\/'<\/span>, routes)\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">const<\/span> httpServer <span style=\"color: #333333;\">=<\/span> http.createServer(router)\r\n\r\nhttpServer.listen(<span style=\"color: #0000dd; font-weight: bold;\">6600<\/span>, ()<span style=\"color: #333333;\">=&gt;<\/span>{\r\n    console.log(<span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>Server is running at port <span style=\"color: #0000dd; font-weight: bold;\">6600<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>);\r\n})\r\n<\/pre>\n<p>This script basically tells node.js to use the routes specified in the app-routes.ts file. Then it should listen for request coming in port 6600<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Setup the Application Structure<\/strong><\/h4>\n<p>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<\/p>\n<p><strong>Step 1<\/strong> &#8211; Create the src folder<\/p>\n<p><strong>Step 2<\/strong> &#8211; Inside the src folder, create 3 folders: services, controllers and routes<\/p>\n<p><strong>Step 3<\/strong> &#8211; Inside the controllers folder, create a file named <em><strong>home-controller.ts<\/strong><\/em>. This would be the content<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> {Request, Response} from <span style=\"background-color: #fff0f0;\">'express'<\/span>;\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #333333;\">*<\/span> <span style=\"color: #008800; font-weight: bold;\">as<\/span> service from <span style=\"background-color: #fff0f0;\">'..\/services\/home-service'<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">const<\/span> getHome <span style=\"color: #333333;\">=<\/span> async (req: <span style=\"color: #333399; font-weight: bold;\">Request<\/span>, res: <span style=\"color: #333399; font-weight: bold;\">Response<\/span>) <span style=\"color: #333333;\">=&gt;<\/span> {\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> res.send(service.goHome());\r\n};\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">export<\/span> <span style=\"color: #008800; font-weight: bold;\">default<\/span> {getHome}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4<\/strong> &#8211; Inside the services folder, create a file named <em><strong>home-service.ts<\/strong><\/em>. This would be the content<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">export<\/span> <span style=\"color: #008800; font-weight: bold;\">function<\/span> goHome() {\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"Kindson is here now yesss\"<\/span><span style=\"color: #333333;\">!<\/span>\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 5<\/strong> &#8211; Inside the routes folder, create a file named<strong><em> app-routes.ts<\/em><\/strong>, this would be the content<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> express from <span style=\"background-color: #fff0f0;\">'express'<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">import<\/span> controller from <span style=\"background-color: #fff0f0;\">'..\/controllers\/home-controller'<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">const<\/span> router <span style=\"color: #333333;\">=<\/span> express.Router();\r\n\r\nrouter.get(<span style=\"background-color: #fff0f0;\">'\/home'<\/span>, controller.getHome);\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">export<\/span> <span style=\"color: #333333;\">=<\/span> router;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>At this point we have setup the API with TypeScript and now you can run the application using the command <em><strong>npm run dev<\/strong><\/em>.<\/p>\n<p>Then you can access the \/home url and see that you get a response!<\/p>\n<p>In the next part, we would then write the services that retrieves data from PostgreSQL database.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[2],"tags":[31],"class_list":["post-256","post","type-post","status-publish","format-standard","hentry","category-node-js-tutorials","tag-typescript"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts\/256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/comments?post=256"}],"version-history":[{"count":7,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts\/256\/revisions"}],"predecessor-version":[{"id":281,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts\/256\/revisions\/281"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/media?parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/categories?post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/tags?post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}