DEV Community

Cover image for NestJs: Deploy to Heroku and Add Swagger Docs
Abayomi Ogunnusi
Abayomi Ogunnusi

Posted on

5 3

NestJs: Deploy to Heroku and Add Swagger Docs

Hello everyone, we'll be building on the puppies api. I strongly recommend you look at the previous post for the 🐶🐶🐶 puppies api.


Agenda

✅ Add Swagger Documentation
✅ Deploying to Heroku


Install the swagger docs and the types

Image description


Import the modules inside the main.ts file and configure it

Image description


Finally, add the ApiProperty to the Entities.

Image description


Let's test our documentation by running yarn start:dev and visiting localhost:7890/api/docs
Image description


Just to be sure our application still works, let's get all the puppies created.
Image description


Heroku Deployment

Login to heroku
Image description


Create an app

Image description


Select overview and Configure Add Ons

Image description


Search for Heroku postgres and click

Image description


Submit the order form

Image description


Click on this to get redirected to the database dashboard

Image description


Click on the view credentials

Image description


Copy the Database Uri and Head to your ormconfig.js file

Image description


orm.config.js

require('dotenv').config();

module.exports = {
  url: process.env.DATABASE_URL,
  type: 'postgres',
  ssl: {
    rejectUnauthorized: false,
  },
  autoLoadEntities: true,
  synchronize: true,
  logging: true,
  entities: ['dist/**/*.entity.js'],
};
Enter fullscreen mode Exit fullscreen mode

Of course, you don't want to hardcode your credentials. We store the Database in the .env file and imported in here.

Now for the actual deployment, follow these steps
Image description

Image description


One last thing to do is to add our env to Heroku's env variable
Image description

Get the source code here.


Conclusion

I hope this post was helpful. Thanks for reading.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay