MOCKSTACKS
EN
Questions And Answers

More Tutorials









NodeJS Using IISNode to host Nodejs Web Apps in IIS

Using an IIS Virtual Directory or NestedApplication via < appSettings >



Using a Virtual Directory or Nested Application in IIS is a common scenario and most likely one that you'll want to take advantage of when using IISNode.

IISNode doesn't provide direct support for Virtual Directories or Nested Applications via configuration so to achieve this we'll need to take advantage of a feature of IISNode that isn't part of the configuration and is much lesser known. All children of the element with the Web.config are added to the process.env object as properties using the appSetting key.

Lets create a Virtual Directory in our < appSettings >

<appSettings>
 <add key="virtualDirPath" value="/foo" />
</appSettings>

Within our Node.js App we can access the virtualDirPath setting

console.log(process.env.virtualDirPath); // prints /foo

Now that we can use the element for configuration, lets take advantage of that and use it in our server code.

/
/ Access the virtualDirPath appSettings and give it a default value of '/'
// in the event that it doesn't exist or isn't set
var virtualDirPath = process.env.virtualDirPath || '/';
// We also want to make sure that our virtualDirPath
// always starts with a forward slash
if (!virtualDirPath.startsWith('/', 0))
 virtualDirPath = '/' + virtualDirPath;
// Setup a route at the index of our app
server.get(virtualDirPath, (req, res) => {
 return res.status(200).send('Hello World');
});

We can use the virtualDirPath with our static resources as well

// Public Directory
server.use(express.static(path.join(virtualDirPath, 'public')));
// Bower
server.use('/bower_components', express.static(path.join(virtualDirPath, 'bower_components')));

Lets put all of that together

const express = require('express');
const server = express();
const port = process.env.PORT || 3000;
// Access the virtualDirPath appSettings and give it a default value of '/'
// in the event that it doesn't exist or isn't set
var virtualDirPath = process.env.virtualDirPath || '/';
// We also want to make sure that our virtualDirPath
// always starts with a forward slash
if (!virtualDirPath.startsWith('/', 0))
 virtualDirPath = '/' + virtualDirPath;
// Public Directory
server.use(express.static(path.join(virtualDirPath, 'public')));
// Bower
server.use('/bower_components', express.static(path.join(virtualDirPath, 'bower_components')));
// Setup a route at the index of our app
server.get(virtualDirPath, (req, res) => {
 return res.status(200).send('Hello World');
});
server.listen(port, () => {
 console.log(`Listening on ${port}`);
});


Conclusion

In this page (written and validated by ) you learned about NodeJS Usin IISNode to host Node.js Web Apps in IIS . What's Next? If you are interested in completing NodeJS tutorial, your next topic will be learning about: NodeJS CLI.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.