Running ASP.NET (not Core) from NPM

I'm currently working on a NodeJS project that makes HTTP calls to an ASP.NET (4.5.1) application. When running locally, I also want to make these calls to my local ASP.NET application. Sometimes, however, I don't want to start up Visual Studio, open the project, compile, and run.

Seeing as this is not ASP.NET Core, I can't just run it from the command-line. Or can I?

It turns out that if you have IIS Express installed (which you should have, with Visual Studio), this is very simple.

You can easily run this command:

C:\Program Files\IISExpress\iisexpress.exe /path:path\to\website\project /port:4000

This will run your website on port 4000.

However, to make it easier to type, I've created an NPM script so I can just run npm start.

In my package.json, I've included the script:

"scripts": {
    "start": "node iis-express.js"
}

In said iis-express.js file, I've put the following script:

var execFile = require('child_process').execFile;

var iisExpressExe = 'C:\\Program Files\\IIS Express\\iisexpress.exe';

var args = [
    '/port:4000',
    '/path:' + __dirname + '\\MyWebsite'
];

var childProcess = execFile(iisExpressExe, args, {});

childProcess.stdout.on('data', function(data) {
    console.log(removeTrailingLinebreak(data));
});

childProcess.stderr.on('data', function(data) {
    console.log(removeTrailingLinebreak(data));
});

var removeTrailingLinebreak = function (input) {
    return input.replace(/\s+$/, '');
}

What this does is create a child process of Node, which will run IIS Express with the correct arguments.

We also capture any output and log it to the console. The removeTrailingLinebreak function is used to remove the newline at the end of IIS' output, otherwise all our output would be separated by empty lines.

So there you go, a simple script to run ASP.NET via NPM.