Search code examples
node.jsiisnode

Create new website in IIS programmatically by nodejs


I want to create a website in IIS programmatically by nodejs. How can I do this?


Solution

  • I found a way and it is editing IIS applicationHost.config xml file by code. this file is in %windir%\system32\inetsrv\Config folder and contains IIS applicationPools and sites data.

    In nodejs, I load that xml file contents with fs and convert to json by xml2js and push my new site and application pool objects to sites and application pools array, then convert all that file json data to xml and write to applicationHost.config file.

    If you want to do this, dont forget to backup applicationHost.config file before adding new website data.

    <applicationPools>
      <add name="myApplicationPool"/>
    </applicationPools>
    
    .
    .
    .
    
    <sites>
      <site name="myWebsite" id="7" serverAutoStart="true">
        <application path="/" applicationPool="myApplicationPool">
          <virtualDirectory path="/" physicalPath="C://projectSites/myWebsite"/>
        </application>
        <bindings>
          <binding protocol="http" bindingInformation="*:80:"/>
        </bindings>
      </site>
    </sites>