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 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.
After building the project with npm run build
Make sure we have an ecosystem.config.js file (see other panel)
To get things started, I limited the max number of instances to 1 - this may be increased later if we need more horsepower.
Install PM2:
As an admin user npm install -g pm2
You'll need to restart the server, but then you should be able to run PM2 start to fire up your instance
Once you close that terminal, your instance will collapse, so we'll need PM2 as a service
Start your webserver with PM2 start like before, then run PM2 save to dump that configuration
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.
From the admin command line - use SC to turn this into a service - type sc.exe create SERVICE_NAME binpath= "SERVICE FULL PATH"
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"