Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP Error 500.1002 - even when using prerender-node #611

Open
TomasKatz opened this issue Nov 22, 2017 · 2 comments
Open

HTTP Error 500.1002 - even when using prerender-node #611

TomasKatz opened this issue Nov 22, 2017 · 2 comments

Comments

@TomasKatz
Copy link

We are trying to set up an IISNODE server with pre-rendering using Universal.
I set up the project the following way with web.config and main server script as follows:
web.config:

<configuration>
   <system.webServer>
     <handlers>
       <add name="iisnode" path="wwwroot/server.js" verb="*" modules="iisnode" />
     </handlers>
     <rewrite>
       <rules>
         <rule name="servermain">
           <match url="/*" />
           <action type="Rewrite" url="wwwroot/server.js" />
         </rule>
       </rules>
     </rewrite>
     <security>
       <requestFiltering>
         <hiddenSegments>
           <add segment="node_modules" />
         </hiddenSegments>
       </requestFiltering>
     </security>
     <iisnode
              node_env="%node_env%"
              nodeProcessCountPerApplication="1"
              maxConcurrentRequestsPerProcess="1024"
              maxNamedPipeConnectionRetry="100"
              namedPipeConnectionRetryDelay="250"
              maxNamedPipeConnectionPoolSize="512"
              maxNamedPipePooledConnectionAge="30000"
              asyncCompletionThreadCount="0"
              initialRequestBufferSize="4096"
              maxRequestBufferSize="65536"
              watchedFiles="*.js;iisnode.yml"
              uncFileChangesPollingInterval="5000"
              gracefulShutdownTimeout="60000"
              loggingEnabled="true"
              logDirectory="iisnode"
              debuggingEnabled="true"
              debugHeaderEnabled="false"
              debuggerPortRange="5058-6058"
              debuggerPathSegment="debug"
              maxLogFileSizeInKB="128"
              maxTotalLogFileSizeInKB="1024"
              maxLogFiles="20"
              devErrorsEnabled="false"
              flushResponse="false"
              enableXFF="false"
              promoteServerVars=""
              configOverrides="iisnode.yml"
             />
   </system.webServer>
 </configuration>

Main script (server.ts):

import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import { renderModuleFactory } from '@angular/platform-server';
import { enableProdMode } from '@angular/core';
import * as express from 'express';
import { join } from 'path';
import { readFileSync } from 'fs';
import * as http from "http";

//  ---------- express routes ---------
import { feedRouter } from "./routes/feed";
import { loginRouter } from "./routes/login";
import { protectedRouter } from "./routes/protected";
import { publicRouter } from "./routes/public";
import { userRouter } from "./routes/user";
import { ngExpressEngine } from '@nguniversal/express-engine';
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
// ----------- normalize port ------------
function normalizePort(val): boolean | number {
    const normalizedPort = parseInt(val, 10);

    if (isNaN(normalizedPort)) {
        // named pipe
        return val;
    }

    if (normalizedPort >= 0) {
        // port number
        return normalizedPort;
    }

    return false;
}
// ----------- server event handlers ------------
function onError(error) {
    if (error.syscall !== "listen") {
        throw error;
    }

    const bind = typeof port === "string"
        ? "Pipe " + port
        : "Port " + port;

    // handle specific listen errors with friendly messages
    switch (error.code) {
        case "EACCES":
            console.error(bind + " requires elevated privileges");
            process.exit(1);
            break;
        case "EADDRINUSE":
            console.error(bind + " is already in use");
            process.exit(1);
            break;
        default:
            throw error;
    }
}
function onListening() {
    const addr = server.address();
    const bind = typeof addr === "string"
        ? "pipe " + addr
        : "port " + addr.port;
}
// ----------- Faster server renders w/ Prod mode (dev mode never needed)------
enableProdMode();
// ============ Express app =================
const port = normalizePort(process.env.PORT || 4000);
const app = express();
const DIST_FOLDER = join(process.cwd(), 'wwwroot');
const template = readFileSync(join(DIST_FOLDER, 'browser', 'index.html')).toString();
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./wwwroot/server/main.bundle');
let server;
// ---------------- ex[ress settings --------
app.set("port", port);
app.engine('html', ngExpressEngine({
  bootstrap: AppServerModuleNgFactory,
  providers: [
    provideModuleMap(LAZY_MODULE_MAP)
  ]
}));
app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, 'browser'));
app.use(require('prerender-node'));
// --------------- api routes ------------
app.use("/api/secure", protectedRouter);
app.use("/api/login", loginRouter);
app.use("/api/public", publicRouter);
app.use("/api/feed", feedRouter);
app.use("/api/user", userRouter);
// Server static files from /browser
app.get('*.*', express.static(join(DIST_FOLDER, 'browser')));

// ALl regular routes use the Universal engine
app.get('*', (req, res) => {
  res.render('index', { req });
});
server = http.createServer(app);
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);

Notes:

  1. The build works when launched trough node straight, with prerendering and all.
  2. When trying to launch the IIS server we get the following error (after disabling dev errors in web.config):
    scnsht

I'll be so happy if this will work, since we're trying to find the perfect solution for SEO prerendering using Universal & Express on IIS.
Many thanks in advance.

@aaroncalderon
Copy link

aaroncalderon commented Mar 28, 2018

@tjanczuk Try setting your rewrite rules as below, you may need to play with the paths a little bit:

     <rewrite>
            <rules>  
                <rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">  
                     <match url="iisnode"/>  
                </rule>  
                <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">                      
                    <match url="^server.js\/debug[\/]?" />  
                </rule>  
                <rule name="StaticContent">  
                     <action type="Rewrite" url="public{{REQUEST_URI}}"/>  
                </rule>  
                <rule name="DynamicContent">  
                     <conditions>  
                          <add input="{{REQUEST_FILENAME}}" matchType="IsFile" negate="True"/>  
                     </conditions>  
                     <action type="Rewrite" url="server.js"/>  
                </rule>  
           </rules>
       </rewrite>

Source YAML configuration support in iisnode

@mlncstr
Copy link

mlncstr commented Oct 17, 2019

Same issue and no combination of web.config files I've found anywhere seem to solve the issue. It's been a couple years @TomasKatz did you find a solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants