Development

How to deploy assets alongside your NestJS API

2 min. read

It's no secret that we are huge NestJS fans here at Tevpro.

We have implemented multiple large-scale projects for clients with Nest being our tool of choice for building robust API's quickly.

Project background

On a recent project, we had the requirement to perform some geolocation queries where we needed to detect the users approximate latitude/longitude based on IP address.

This task would be simple using the non-intrusive technology, ip2location.com, that identifies geographical location and proxy information by IP address. We opted for the free version which meant we needed to deploy a local database (single BIN file) along with our API.

And since you won't find the instructions to do so in Nest documentation, we thought we'd share a quick tutorial to show you how to easily deploy assets alongside a NestJS API.

Getting started

While you can setup a npm script to copy assets over and add to your build script, we choose a route that most Angular developers should be familiar with.

Start by opening up your nest-cli.json file located in the root of your application.

Here is an example from a freshly created Nest application:

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
}
Before: nest-cli.json

The missing piece is to tell the compiler to copy over the files from the directory you choose. Keeping with the Angular theme, I created an assets folder and stored the database BIN file there.

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "assets": ["assets/**/*"]
  }
}

After: nest-cli.json

After making this modification, anytime our application is built, anything inside the assets folder will be copied over to the dist folder and can be deployed alongside the API.

Conclusion

And there you have, what I think, is the simplest way to ensure any assets you need deployed alongside your API are done with ease.

At Tevpro, we work with companies of all shapes and sizes to automate and optimize business processes.  I hope you found this article helpful and if you have any other coding challenges, feel free to reach out on Twitter or via email with any questions.

Photo by John Schnobrich

Justin Waldrip

Angular • React • Ionic • NestJS • C# • Azure • Next.js