<?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>DEV Community: abdulrahilsheikh</title>
    <description>The latest articles on DEV Community by abdulrahilsheikh (@abdulrahilsheikh).</description>
    <link>https://dev.to/abdulrahilsheikh</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%2F1099426%2F2447827d-4bf7-4be8-ab21-f5d11131b580.jpeg</url>
      <title>DEV Community: abdulrahilsheikh</title>
      <link>https://dev.to/abdulrahilsheikh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abdulrahilsheikh"/>
    <language>en</language>
    <item>
      <title>Creating an Express.js App Using Typescript &amp; MongoDb P-1</title>
      <dc:creator>abdulrahilsheikh</dc:creator>
      <pubDate>Wed, 05 Jul 2023 10:38:18 +0000</pubDate>
      <link>https://dev.to/abdulrahilsheikh/creating-an-expressjs-app-using-typescript-mongodb-p-1-1ha3</link>
      <guid>https://dev.to/abdulrahilsheikh/creating-an-expressjs-app-using-typescript-mongodb-p-1-1ha3</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Section 1 - Creating Express App&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step by step Implementation:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Step 1 :&lt;/strong&gt; First create a empty folder and open terminal in this folder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2 :&lt;/strong&gt; Run the following command in your terminal, to create a nodejs application, because our express server will work inside the node application.&lt;br&gt;
&lt;em&gt;Command :&lt;/em&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 3 :&lt;/strong&gt; Installing all the necessary dependencies for our application.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Command :&lt;/em&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install express cors mongoose
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 4 :&lt;/strong&gt; Now we need to install types for express and cors.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @types/express @types/cors --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 5 :&lt;/strong&gt; Now we have to set up typescript for our app for that run the following command&lt;br&gt;
&lt;em&gt;Command :&lt;/em&gt;     &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tsc --init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;This will create a tsconfig.json file which will have all the configuration for the typescript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6 :&lt;/strong&gt; Now to run the typescript application we need to install some additional dependencies. Which are ts-node and nodemon.&lt;br&gt;
 &lt;em&gt;Command :&lt;/em&gt;    &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i nodemon ts-node --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;After following all the steps your folder structure should look like&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GrFACh78--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wgaq3k9v53sdus0ck9cs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GrFACh78--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wgaq3k9v53sdus0ck9cs.png" alt="Folder structure" width="279" height="104"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-|-node_modules
 |-package-lock.json
 |-package.json
 |-tsconfig.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 7 :&lt;/strong&gt; Creating an entry point for our express app to start.&lt;br&gt;
create an index,ts file in the root of your project. Also create a folder with app name here we will put all of our middleware, routes etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The project structure will look like following.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ypQNmo7N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/898bifbc49e5v4haot9r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ypQNmo7N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/898bifbc49e5v4haot9r.png" alt="Image showing folder structure 2" width="286" height="175"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-|-app
 |-node_modules
 |-index.ts
 |-package-lock.json
 |-package.json
 |-tsconfig.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Step 8:&lt;/strong&gt; To run the server we need to add following script to our package.json file. Also change the extension on index.js to index.ts of the main property in package.json. &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"run:dev": "nodemon index.ts"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;After this our script will look like&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PrQLWTox--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ldivk85btbqv22gg8qjc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PrQLWTox--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ldivk85btbqv22gg8qjc.png" alt="Package Json after editing" width="591" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 9:&lt;/strong&gt; Now to test our setup is working file put the below code snippet in the index.ts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("I'm running")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in the terminal run the following 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 run run:dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The text &lt;strong&gt;I'm running&lt;/strong&gt; should log in the terminal and your terminal should look like following.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dVdlTcvh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lrzdcycgfm7t7u3t4qgu.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dVdlTcvh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lrzdcycgfm7t7u3t4qgu.jpg" alt="Server run 1" width="609" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hurray 🎉🎊🎉 we have successfully setup our starting point&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 10:&lt;/strong&gt; Now create a app.ts file in app folder. The folder structure will look like following.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rj7xpeyn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjbbsz306kbksnoass6f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rj7xpeyn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjbbsz306kbksnoass6f.png" alt="folder structure 3" width="272" height="212"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-|-app
 |   |-app.ts
 |-node_modules
 |-index.ts
 |-package-lock.json
 |-package.json
 |-tsconfig.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 11:&lt;/strong&gt; Now need to create our express server. For that we need an instance of express. For that we need to import express from express package we installed. for that put the below code at the top of app.ts file.&lt;br&gt;
And now invoke this express function imported and store it into a variable named app&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 from "express";

const app: express.Application = express();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;express.Application will give us typescript interface of express&lt;/p&gt;

&lt;p&gt;Now create a function named server and export it. We will use this server function in index.ts.&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 from "express";

const app: express.Application = express();
export const server = async () =&amp;gt; {
    const port: number = 3500;
    app.listen(port, () =&amp;gt; {
        console.log(`Server running on ${port}`);
    });
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in index.ts import the server and invoke it as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { server } from "./app/app";

server();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now again run the npm run run:dev to start the server in dev mode.&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 run:dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your terminal will look like following.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iPjtYPxZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/timatk3it03fhvmivkn4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iPjtYPxZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/timatk3it03fhvmivkn4.png" alt="Server run 2" width="512" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 12:&lt;/strong&gt;  Now we will create a basic get route which will return hello world. For doing first we will create an organized folder structure . For that we will create a modules folder inside our app folder for holding our routes. In this modules folder create a new folder called users. Inside it create files called users.routes.ts, users.schema.ts and users.services.ts  . Now you folder should look like following.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Pdy8QYMy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6i32r7sr29kp40iymfso.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Pdy8QYMy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6i32r7sr29kp40iymfso.png" alt="folder structure 3" width="242" height="300"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-|-app
 |   |-modules
 |   |    |-users
 |   |       |-users.routes.ts
 |   |       |-users.schema.ts
 |   |       |-users.services.ts
 |   |-app.ts
 |-node_modules
 |-index.ts
 |-package-lock.json
 |-package.json
 |-tsconfig.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Each file has its own role. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; users.routes.ts will hold all the routes such as get, post, patch etc.&lt;/li&gt;
&lt;li&gt;users.schema.ts will hold the mongoose schema and model which we will use to communicate with mongodb.&lt;/li&gt;
&lt;li&gt;users.services.ts will hold the all function which we need to complete or process the user request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 13:&lt;/strong&gt; In user.routes.ts import the Router from express and create an instance of it.&lt;br&gt;
Router function is used to create a new router object which is used to create a new router object in your program to handle requests. &lt;br&gt;
Your code should look like below.&lt;br&gt;
&lt;/p&gt;

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

const userRouter = Router();

userRouter.get("/users", (req, res) =&amp;gt; {
    res.json({ message: "sent succesfully" });
});

export default userRouter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now import this user router in your app.ts file and use is as shown below.&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 from "express";
import userRouter from "./modules/users/users.routes";

const app: express.Application = express();
export const server = async () =&amp;gt; {
    const port: number = 3500;
    app.use('/',userRouter);
    app.listen(port, () =&amp;gt; {
        console.log(`Server running on ${port}`);
    });
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in your browser go to http:localhost:3500/users or whatever your port is http:localhost:/users.&lt;br&gt;
You will see the following.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QQpLpeR0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eriyzgx8kylgjis2601h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QQpLpeR0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eriyzgx8kylgjis2601h.png" alt="Browser view 1" width="511" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations 🎊🎉 you've successfully created a route. You can similarly create a post route.&lt;/p&gt;

&lt;p&gt;Now we will use the services file.&lt;br&gt;
The services file will have all the business logic like processing data, fetching from db etc.&lt;br&gt;
In users.services.ts paste the following code block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const users = [
    { name: "Jhon Doe", age: 24 },
    { name: "Man Freed", age: 22 },
    { name: "Mr Beast", age: 21 },
    { name: "Carry Minati", age: 25 },
];

export const getRandomUser = () =&amp;gt; {
    const index = Math.floor(Math.random() * users.length);
    return users[index];
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also modify the user.routes.ts like the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Router } from "express";
import { getRandomUser } from "./users.services";

const userRouter = Router();

userRouter.get("/users", (req, res) =&amp;gt; {
    const user = getRandomUser();
    res.json({ user });
});

export default userRouter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now each time you hit "/users" or refresh the page in browser you will get a different user.&lt;br&gt;
We will be using this services a lot when dealing with mongodb.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z0R2sn-1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlmuhekd2d0wbjn3f937.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z0R2sn-1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rlmuhekd2d0wbjn3f937.png" alt="Browser view 2" width="335" height="135"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rNsvlH9K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6f6nga2joqp106imo2e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rNsvlH9K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6f6nga2joqp106imo2e.png" alt="Browser view 3" width="324" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For connecting to mongodb and using it we will have another blog. &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
