<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Manmeet Singh</title>
    <description>The latest articles on Forem by Manmeet Singh (@manmeetsingh1012).</description>
    <link>https://forem.com/manmeetsingh1012</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F970280%2Feb57529e-aefe-412c-a57f-a3350984cf73.jpeg</url>
      <title>Forem: Manmeet Singh</title>
      <link>https://forem.com/manmeetsingh1012</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/manmeetsingh1012"/>
    <language>en</language>
    <item>
      <title>How to Easily Set Up an Express API with TypeScript</title>
      <dc:creator>Manmeet Singh</dc:creator>
      <pubDate>Wed, 31 Jul 2024 09:16:02 +0000</pubDate>
      <link>https://forem.com/manmeetsingh1012/how-to-easily-set-up-an-express-api-with-typescript-3flp</link>
      <guid>https://forem.com/manmeetsingh1012/how-to-easily-set-up-an-express-api-with-typescript-3flp</guid>
      <description>&lt;h5&gt;
  
  
  Introduction
&lt;/h5&gt;

&lt;p&gt;In this tutorial, we will see how to create a basic Express Node.js app using TypeScript. Setting up TypeScript involves several steps, and missing any step can lead to frustrating errors. So let's begin! &lt;/p&gt;

&lt;h5&gt;
  
  
  Prerequisites
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Familiarity with nodejs &lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  Step 1:
&lt;/h5&gt;

&lt;p&gt;Create A empty folder and that folder in the vs code.&lt;/p&gt;

&lt;h5&gt;
  
  
  Step 2:
&lt;/h5&gt;

&lt;p&gt;Open terminal and type first command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this will initialize package.json file &lt;/p&gt;

&lt;h5&gt;
  
  
  Step 3:
&lt;/h5&gt;

&lt;p&gt;Install this dependencies :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express typescript ts-node @types/node @types/express nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ts-node: This is a TypeScript execution engine  for Node.js. It allows you to run TypeScript directly without having to compile it to JavaScript first.&lt;/p&gt;

&lt;p&gt;@types/node: This package provides type definitions for Node.js. It allows TypeScript to understand the types used in Node.js&lt;/p&gt;

&lt;p&gt;nodemon: This is a utility that monitors for any changes in your source and automatically restarts your server&lt;/p&gt;

&lt;p&gt;@types/express: This package provides type definitions for Express. It helps TypeScript understand the types used in Express&lt;/p&gt;

&lt;h5&gt;
  
  
  Step 4:
&lt;/h5&gt;

&lt;p&gt;Genrate ts.config file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tsc --init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Step 5 :
&lt;/h5&gt;

&lt;p&gt;You will see outdir at line 58 in compiler options in tsconfig file outdir replace it will this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"outDir": "./dist"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Step 6:
&lt;/h5&gt;

&lt;p&gt;Add this in your tsconfig file after the compiler options :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"include": ["src/**/*.ts"],
"exclude": ["node_modules", "dist"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Step 7:
&lt;/h5&gt;

&lt;p&gt;Add this into your package.json script section&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"start": "ts-node src/index.ts",
"dev": "nodemon src/index.ts"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Step 8:
&lt;/h5&gt;

&lt;p&gt;Create src folder in that folder create index.ts file and this basic express API&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import express, { Request, Response } from "express";

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

app.use(express.json());

app.get("/", (req: Request, res: Response) =&amp;gt; {
  res.send("Hello, world!");
});

app.listen(port, () =&amp;gt; {
  console.log(`Server is running on http://localhost:${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run your code :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you will see this in your terminal: Server is running on &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; when you will click on this you will redirected to the browser and you will see hello world.&lt;/p&gt;

&lt;p&gt;Ok so this was a easiest way of creating a express app in ts . if you find it useful please like and comment it will motivate me to create more such blogs.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>express</category>
      <category>node</category>
      <category>api</category>
    </item>
  </channel>
</rss>
