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;// use spawn instead, see Kevin's comment below var execFile = require('child_process').spawn; 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+$/, ''); }
Update: as Kevin remarks in the comments below, it’s better to use spawn instead of exec.
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.
2 Responses
Thanks for this tip! Even though it’s a bit older now, I needed this recently for setting up some new Playwright tests against our older ASP.Net application (not .Net Core).
In case anybody else stumbles on this post and runs into the same issue I did, it turns out there’s a problem with using Node’s execFile API for this. That API buffers the stdout coming from the child process, and has a default limit of 1MB. You can change it, but for something like IIS Express, it’s possible you’re going to get a *lot* of output. In our case we’re running a bunch of Playwright tests, so there is a ton of request/response logging output sent to stdout.
After a bit of digging into that issue, I found that there’s a different Node API recommended if you expect the child process to produce a lot of output like that. The suggestion is to use spawn instead of exec (or execFile).
https://nodejs.org/api/child_process.html#child_processspawncommand-args-options
Hope this helps somebody else!
Thanks for the improvement! I’ll update the article.