1

i have an Nest.js application that i want to run on windows IIS Server. I installed allready the iisnode module github.com/tjanczuk/iisnode and created a new website and application on IIS

I am Able to call my Api endpoints but getting following Message:

Warning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

The Problem is that i dont use Buffer() im my code its only used in some Node-Modules used within my project.

Before i spent more time on solving the deprecated warning i just wanted to be sure, wheter runnig nest.js applications on IIS with iisnode is possible in general?

Thanks a lot for your help

This is my web.config file

<configuration>
  <system.webServer>

    <!-- indicates that the hello.js file is a node.js application 
    to be handled by the iisnode module -->

    <handlers>
      <add name="iisnode" path="main.js" verb="*" modules="iisnode" />
    </handlers>
<rewrite>
      <rules>
        <!-- Do not interfere with requests for node-inspector debugging -->
        <rule name="sendToNode">
          <match url="/*" />
      <action type="Rewrite" url="main.js" />
        </rule>
      </rules>
    </rewrite>
  ...

  </system.webServer>
</configuration>
'''
Lex Li
  • 60,503
  • 9
  • 116
  • 147
dev_EEAN
  • 21
  • 1
  • 4
  • 1
    IISNode was developed to serve Node.js web apps on Azure App Service when there was only Windows OS there. However, Azure App Service now supports Linux/Docker fully, so such projects (Node.js/Python on IIS) are out-of-maintenance (https://github.com/Azure/iisnode has been idle for two years). Effectively that's a hint you shouldn't attempt to run Node.js on IIS. Can you switch to Linux? – Lex Li Apr 17 '20 at 15:01
  • Hi Lex Li , thanks for your quick answer. No, unfortunatly not :-( – dev_EEAN Apr 17 '20 at 15:06
  • your error is not related to iis. based on warning new Buffer() should be replaced with one of:Buffer.alloc(), Buffer.allocUnsafe() or Buffer.from(). [link1](https://stackoverflow.com/questions/52165333/deprecationwarning-buffer-is-deprecated-due-to-security-and-usability-issues) ,[link2](https://stackoverflow.com/questions/53046414/deployment-on-iis-of-nestjs-application) – Jalpa Panchal Apr 20 '20 at 09:00

3 Answers3

1

Start From zero

Install urlrewrite fist if not

Here We are going a crerate hello world project and host in iis server

So Install nest globaly your preference i am using this approach

npm install -g @nestjs/cli

So Create new nest project

nest new myhost

enter image description here

Then Move to that folder by this=>


cd myhost
npm run start

enter image description here

Here i change 3000 port to 4444 you can change as your wish

enter image description here

Just build your Project and now dist folder created

 npm run build

enter image description here

Now install pm2 node package this package as background service pm2link

npm install pm2 -g

enter image description here Now you Can check the link http://localhost:4444/

enter image description here

Now Open iis (internet information service)

==> Add Website
        ==>Add site name
        ==>Add Physical path as you build folder 
        ==> Add Port not 4444 because our node project running on that port so use different port

enter image description here

Now Click on url rewrite for redirect port 4445 (iis hosted port) to 4444(project running port)

enter image description here

Select Reverse proxy

enter image description here

Add reverse proxy rule

enter image description here

Now check your browser work on both port

enter image description here web.config may like this above config enough

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://localhost:4444/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration> 
lava
  • 6,020
  • 2
  • 31
  • 28
0

In case anyone is still in search of an answer on how to host NestJs application on IIS, you have to make use of URLRewrite and PM2 (IISNode is not required):

Install URLRewrite module in your IIS.

Install PM2

The idea is to run your NestJs application via PM2 and then setup a reverse proxy in IIS that redirects all inbound requests to the running NestJS application.

First make sure your application is running via PM2. Navigate to the folder where your main.js file is, and run pm2 start main.js

Then, configure NestJS application to run on a port of your choice in IIS.

Then, setup a reverse proxy setting in web.config that redirects to the actual port that your NestJs application is running on. My web.config looks like this, where I'm redirecting the inbound request to localhost:3333

<?xml version="1.0" encoding="UTF-8"?>
 <configuration>
  <system.webServer>
    <rewrite>
        <rules>
            <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                <match url="(.*)" />
                <action type="Rewrite" url="http://localhost:3333/{R:1}" />
            </rule>
        </rules>
    </rewrite>
  </system.webServer>
</configuration>

And that's it. Your NestJs application will now successfully run on IIS.

Refer this wonderful link for more information.

David Buck
  • 3,752
  • 35
  • 31
  • 35
0
  1. In order to remedy the warning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

Under "C:\Program Files\iisnode" modify the "interceptor.js" file at line 169. Replace return new Buffer(data, typeof encoding === 'string' ? encoding: 'utf8'); by return Buffer.from(data, typeof encoding === 'string'? encoding: 'utf8');

That will solved this problem caused by iisnode itself.

  1. In order to launch a Nestjs application via iisnode only (i.e. without resorting to the reverse proxy).

First, be sure "URLRewrite" is installed and your "web.config" file is as follows :

    <configuration>
      <system.webServer>
        <handlers>
          <add name="iisnode" path="main.js" verb="*" modules="iisnode" />
        </handlers>
    
        <rewrite>
          <rules>
            <rule name="HTTP to Prod HTTPS redirect" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
                <add input="{HTTPS}" pattern="off" ignoreCase="true" />
              </conditions>
              <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
            </rule>
            <!-- Don't interfere with requests for logs -->
            <rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
              <match url="^[a-zA-Z0-9_\-]+\.js\.logs\/\d+\.txt$" />
            </rule>
            <!-- Don't interfere with requests for node-inspector debugging -->
            <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
              <match url="^main.js\/debug[\/]?" />
            </rule>
            <!-- First we consider whether the incoming URL matches a physical file in the public folder -->
            <rule name="StaticContent">
              <action type="Rewrite" url="public{REQUEST_URI}" />
            </rule>
            <!-- All other URLs are mapped to the Node.js application entry point -->
            <rule name="DynamicContent">
              <conditions>
                 <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
              </conditions>
              <action type="Rewrite" url="main.js" />
            </rule>
          </rules>
        </rewrite>
    
      </system.webServer>
    </configuration>

Then, modify your "main.ts" file as follows :

import { NestFactory } from '@nestjs/core';
import * as dotenv from 'dotenv';
import * as process from 'process';
import { AppModule } from './app.module';

const normalizePort = (val) => {
  const port = parseInt(val, 10);
  if (isNaN(port)) {
    return val;
  }
  if (port >= 0) {
    return port;
  }
  return false;
};

dotenv.config();

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const port = normalizePort(process.env.PORT || process.env.APP_PORT);

  await app.listen(port);
}
bootstrap();

Note the use of process.env.PORT delivered by IIS site binding and process.env.APP_PORT from .env file as follows :

# SERVER
APP_PORT=3000
Gaetan LOISEL
  • 263
  • 3
  • 7