NestJS app
There is so much we can talk about when you pick up a beautiful framework to build an app. Slowly our Oblog will drift away from the beautiful framework we started with – NestJS. This is the beginning of that drift.
Certainly, you have come across the word production ready. The readiness indicates the app is robust and stable enough to be exposed to users. However, the word deployment ready is a word we felt necessary to be accomplished in the modern world of app supply chain i.e. DevOps processes.
Hands On – First dispatch
We would generally see the following piece of code in the main.ts file –
1: async function bootstrap() {
2: const app = await NestFactory.create(ApplicationModule);
3: await app.listen(3000);
4: }
5:
6: bootstrap();
Table 1 main.ts code snippet
Line #3 indicates the port at which the application will listen. This is fairly simple and works well in developer machine or in a machine used by closed team. However, when you intend to launch the application to wider audiences you might want to launch this behind firewalls and load balancers. Probably even run within an existing server infrastructure. Let us peek into how to do this in say IIS server or deploy the app in Azure Functions.
It is very simple. You are going to cruise through these at no time. TL; DR jump to Instructions for Hands-On sections.
Deploying in IIS
To deploy a NestJS application you will node to be running within IIS. This can be achieved by installing the iisnode module. Before you pull the module and install in IIS let us feast on why would you do that in the first place.
Why would you do this?
Deploying to IIS will require you to use a module (in IIS parlance) called iisnode. You can retrieve the binary from here – https://github.com/Azure/iisnode/releases. This is a native IIS module which claims to offer the benefits of
1. node.exe process management with improved reliability
2. Offers the feature to blend your NestJS application with ASP.NET, vanilla JS, CSS, and ASP.NET Core and many other languages that IIS support all within a single website i.e. single URL.
3. Scale across CPU’s without actually putting a load balancer the module can span multiple node.exe and balance load amongst them. Thus, letting your NestJS run virtually on multiple instances.
4. In remote machines accessing node process log was difficult and you had to use SSH, you could with this module; access logs over http. Add a certificate you can do that over https as well.
5. Minimal impact on existing code of NestJS.
6. Benefits of IIS tags along with this module e.g. port sharing, URL rewriting, compression, caching etc.
Instructions for Hands-On
In the file package.json you will notice a script command of this sort –
1: {
2: …
3: “scripts” : {
4: …
5: “prestart:prod” : “npm run build”,
6: …
7: }
8: …
9: }
Table 2 – package.json snippet
For whatever reason you did not had the script command in Line #5 of above snippet, copy it to your package.json file. You should also install the iisnode module mentioned few scrolls up. Once you have done these you could scroll down for the steps –
1. Change the Line #3 in Table 1 main.ts code snippet to await app.listen(process.env.PORT | | 3000);
2. Run the command npm run prestart:prod
3. You will notice a dist folder in the same location where your source code resides. Compress the dist folder to a zip archive file. Use your favourite zipping program.
4. You can XCOPY or use any other means to ship the zip file to the IIS server folder system. If you want to know about the XCOPY command you can check this doc .
5. Decompress the zip file to a folder
6. Add the web.config file with following content –
1: <configuration>
2: …
3: <system.webServer>
4: <handlers>
5: <add name=”iisnode” path=”main.js” verb=”*” modules=”iisnode” />
6: </handlers>
7: </system.webServer>
8: …
9: </configuration>
Table 3 – web.config snippet
It is this file which instructs IIS to use iisnode for any request that comes across for the main.js file with any of HTTP POST, PUT, DELETE, GET etc.
7. Hit the URL in the browser. You should see your application there.
Additionally, note that we have given the file name which is only entry point. You can have your any other JS files e.g. angular UI library or jQuery or YUI library etc. which will be still be rendered as static js file and not be treated via the iisnode module. You can discover more in this post in Medium .Deploying in Azure Functions
There is a cool adapter being developed by the NestJS developers. You can read more about it here . This adapter creates a HTTP module of Azure Functions for Nest.
This is going to be cake walk as well similar to what we did in IIS. Except there are two modules that you will need to proceed. Implicitly we assume you have Azure credentials and have the subscription active to create Azure functions.
Let us begin in a terminal and follow the instructions below –
1. Issue the command nest add @nestjs/azure-func-http make sure you run this command at the absolute root directory where you have your amazing NestJS application. This command should add few files and update the package.json file.
2. After this you could spend some time inspecting the new files. Let me add a marker for talking about these files in a future Oblog dispatch.
3. To deploy this to Azure functions there are few approaches. Easiest of all is yet to appear and we will have to watch the space of trilon.io for a command of the sort nest deploy azure. However, for now we have two options. First is via command prompt or PowerShell another is via VSCode. We will share the instructions to do via VSCode here.
4. You will have to install a VSCode extension called “Azure Functions”.
5. Login to Azure by clicking the newly installed extension’s pane – “Sign in to Azure…” link.
6. Once you are signed-in you will have the access to command i.e. one of the extension commands which will be activated is “Deploy to Function app”. You could access it from the panel or you could as well access it from the Ctrl+Shift+P command palette. You can issue the command by typing > Deploy to function app in the palette.
7. You will be prompted to “Create New Function App in Azure” if you did not have any functions already running otherwise you could select the name of a function if you want it to be updated.
8. Provide a unique name for the function and wait for the deployment to finish.
9. Once deployed hit the function URL you should see your amazing NestJS API.
Au revoir !
We now will let you enjoy your amazing app in NestJS and let you share it with as wider audience as possible. In parting thoughts, this dispatch is not to communicate the notion that you can only deploy NestJS in either IIS or as Azure Functions. This post demonstrates the actions that you need to deploy in those environments. As we part we also will like to highlight there are plenty of other server processes to which you can deploy the NestJS app. Nginx, AWS BeanStalk, AWS Lambda, Heroku are few to name. Each of them will have subtilities similar to the ones we followed to deploy the application to IIS / Azure Functions.
Happy coding in NestJS!