Nuxt Alongside IIS

The back story

Once upon a time, everything was written in .NET - applications were monolithic and complex, but everything was compiled which meant it was secure and fast, right?  But eventually the dreaded Open Source community caught up - using the magical Java Script - but even they were not able to just slay the .NET beast immediately.  So instead, they needed a solution where new JS projects could run alongside the existing .NET applications, delivering a seamless experience to the user and allowing the dev team to slowly replace that old functionality.  Here's how we did that...

The tech

The original application is written in .NET, running on an IIS server under ourdomain.com - the first job is to make sure that we are not impacting that original service, but we WOULD like to serve our new application as ourdomain.com/newstuff

First up is getting our Nuxt application up and running with CI/CD - that's covered in another article on here.  In this case, we're running that Nuxt instance on port 3030 (accessible from localhost only) and using IIS as a reverse proxy.

Running Nuxt

  1. After building the project with npm run build

  2. Make sure we have an ecosystem.config.js file (see other panel)

    1. To get things started, I limited the max number of instances to 1 - this may be increased later if we need more horsepower.

  3. Install PM2:

    1. As an admin user npm install -g pm2

    2. You'll need to restart the server, but then you should be able to run PM2 start to fire up your instance

    3. Once you close that terminal, your instance will collapse, so we'll need PM2 as a service

  4. Start your webserver with PM2 start like before, then run PM2 save to dump that configuration

  5. Create a BAT file that we'll build into our service - it just needs to run PM2 resurrect to restore whatever you've put into your dump file above.

  6. From the admin command line - use SC to turn this into a service - type sc.exe create SERVICE_NAME binpath= "SERVICE FULL PATH"

  7. If you go to Windows Services, you should see your new service and be able to set it for automatic start, which means your web application should be automatically relaunched with PM2 if the server reboots.

ecosystem.config.js

module.exports = {  

apps: [     {       name: 'ourNewApp',       exec_mode: 'cluster',       instances: '1', // Or a number of instances       script: './node_modules/nuxt/bin/nuxt.js',       args: 'start'     }   ] }  

 

pm2_startup.bat

@echo off set

HOMEDRIVE=F:\Code\NuxtTAC\node_modules\.bin

set PM2_HOME=c:\etc\.pm2

@REM Optionally, you can add 'pm2 kill' just before 

@REM  resurrect (adding a sleep between 2 commands):

@REM      pm2 kill

@REM      timeout /t 5 /nobreak > NUL

@REM      pm2 resurrect

@REM otherwise, you can simple call resurrect as follows:

%HOMEDRIVE%\pm2 resurrect

echo "Done"