DEV Community

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

Posted on

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.

Top comments (0)