<?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: Bilal Ur Rehman</title>
    <description>The latest articles on DEV Community by Bilal Ur Rehman (@bilalurrehman27).</description>
    <link>https://dev.to/bilalurrehman27</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%2F1553488%2F5456f3b1-e399-4790-a2f8-cde5858c6d2a.jpeg</url>
      <title>DEV Community: Bilal Ur Rehman</title>
      <link>https://dev.to/bilalurrehman27</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bilalurrehman27"/>
    <language>en</language>
    <item>
      <title>Deploying a NestJS Application on Vercel with a PostgreSQL Database</title>
      <dc:creator>Bilal Ur Rehman</dc:creator>
      <pubDate>Mon, 03 Jun 2024 07:56:38 +0000</pubDate>
      <link>https://dev.to/bilalurrehman27/deploying-a-nestjs-application-on-vercel-with-a-postgresql-database-2be0</link>
      <guid>https://dev.to/bilalurrehman27/deploying-a-nestjs-application-on-vercel-with-a-postgresql-database-2be0</guid>
      <description>&lt;p&gt;Deploying a NestJS Application on Vercel with a PostgreSQL Database&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites:-&lt;/strong&gt;&lt;br&gt;
This article is for someone who already knows the nest.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Familiarity with Nest.js&lt;/strong&gt;&lt;br&gt;
Nest.js application connected and working locally with PostgreSQL&lt;br&gt;
Vercel account to deploy your application&lt;br&gt;
Vercel and Supabase accounts&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Database Setup:-&lt;/strong&gt;&lt;br&gt;
Login to Supabase to obtain database credentials: Hostname, Database Name, Username, and Password.&lt;/p&gt;

&lt;p&gt;Try to connect your application with the credentials locally to make sure everything is working fine.&lt;/p&gt;

&lt;p&gt;You will get this under the settings under&lt;br&gt;
/settings/database section&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjvys6thhzmyu5h090be1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjvys6thhzmyu5h090be1.png" alt="Supabase database section"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prepping for Vercel Deployment:-&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now create vercel.json file in the root of your application and add the following configuration in to it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

{
  "version": 2,

  "builds": [
    {
      "src": "src/main.ts",
      "use": "@vercel/node"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "src/main.ts",
      "methods": ["GET", "POST", "PUT", "PATCH" , "DELETE"]
    }
  ]
}


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

&lt;/div&gt;

&lt;p&gt;It should look like something like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyvfwmg9wqvq2yy4mkqw7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyvfwmg9wqvq2yy4mkqw7.png" alt="Vercel.json"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project Setup on Vercel:-&lt;/strong&gt;&lt;br&gt;
Now login/signup to your vercel account and create a new project there &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqe9dt4l9q02qv7qq00ee.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqe9dt4l9q02qv7qq00ee.png" alt="Vercel Dashboard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Import your git repository and connect your github repository&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr4syw1fp2tp8pmji2rfm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr4syw1fp2tp8pmji2rfm.png" alt="Import your repo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Configure your project:-&lt;/strong&gt;&lt;br&gt;
Now configure your project i.e. name, enter the environment variables if any and then hit deploy.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjd2pb6b077qe1gmypfd8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjd2pb6b077qe1gmypfd8.png" alt="Deployment"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that’s it. Wait for deployment and test using the provided link.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1xu5h2gsir323r3mok6x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1xu5h2gsir323r3mok6x.png" alt="Build succeed"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Swagger UI Loading Issue Fix:-&lt;/strong&gt;&lt;br&gt;
You might not be able to see your swagger docs on your deployed nest.js app, as vercel was unable to load the swagger css. For that you need to load the swagger css with external css,&lt;br&gt;
Here is the sample code which shoes, how to do it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

SwaggerModule.setup("docs", app, document, {
    customSiteTitle: "Api Docs",
    customfavIcon: "https://avatars.githubusercontent.com/u/6936373?s=200&amp;amp;v=4",
    customJs: [
      "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.15.5/swagger-ui-bundle.min.js",
      "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.15.5/swagger-ui-standalone-preset.min.js",
    ],
    customCssUrl: [
      "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.15.5/swagger-ui.min.css",
      "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.15.5/swagger-ui-standalone-preset.min.css",
      "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.15.5/swagger-ui.css",
    ],
 });


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

&lt;/div&gt;

&lt;p&gt;And that’s it.&lt;/p&gt;

&lt;p&gt;Now you can access your nest.js application on vercel.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Issue With Helmet:-&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For all those who are using helmet might not be able to see the swagger page as helmet blocks the request.&lt;/p&gt;

&lt;p&gt;Now the hack is just initialize the helmet after the swagger is initilized, as in node file is read from top to bottom then the request won’t get’s blocked and you will got to see segger docs.&lt;/p&gt;

&lt;p&gt;This is how your main file will look like.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

 SwaggerModule.setup("docs", app, document, {
    customSiteTitle: "Api Docs",
    customfavIcon: "https://avatars.githubusercontent.com/u/6936373?s=200&amp;amp;v=4",
    customJs: [
      "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.15.5/swagger-ui-bundle.min.js",
      "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.15.5/swagger-ui-standalone-preset.min.js",
    ],
    customCssUrl: [
      "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.15.5/swagger-ui.min.css",
      "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.15.5/swagger-ui-standalone-preset.min.css",
      "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.15.5/swagger-ui.css",
    ],
  });

  app.use(helmet());      //here


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Follow Me on GitHub&lt;/strong&gt;&lt;br&gt;
If you found this article helpful or interesting, consider following me on &lt;a href="https://github.com/BilalurRehman-27" rel="noopener noreferrer"&gt;GitHub &lt;/a&gt;for more updates, projects, and resources related to NestJS and other technologies. You can find my GitHub profile &lt;a href="https://github.com/BilalurRehman-27/nestjs-jwt-auth-postgres-type-orm" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That’s it.&lt;/p&gt;

&lt;p&gt;Happy Coding !&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>vercel</category>
      <category>typescript</category>
      <category>helmet</category>
    </item>
  </channel>
</rss>
