繁体   English   中英

如何使用 IISNode 从 IIS 为 Node Express 提供 React 前端

[英]How to serve Node Express with React frontend from IIS using IISNode

我正处于项目的最后阶段。 我有一个从 NodeJS Express 运行的运营网站,它将为构建的 create-react-app 前端提供服务。 现在我需要使用 IISNode 将其配置为从 IIS 运行。 我遵循了许多方向,但没有成功。 IIS 的原因是只要服务器(Windows server 2012 R2,IIS 8)正在运行,该站点将在没有用户登录且可用的情况下提供服务。

我读过许多人们用来让自己的网站运行的“黑客”。

节点正在监听 process.env.PORT || 8000. 当我直接从节点(npm run start)运行时,没有任何困难,但是当我尝试从 IIS 执行相同操作时,它给了我查找后端 URL 的错误。 当尝试将 React 作为单独的站点运行时,我在搜索初始 API 时遇到 404 错误,而在使用托管在“公共”文件夹中的前端运行它时,我得到 403.14:无法列出目录内容。

我目前什至不知道要显示哪个代码来解决这个问题,但我怀疑 web.config 可能是其中的一部分。 我将它与来自节点的 bin/www 文件一起包括在内。

//   /bin/www

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('node-backend:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '8000'); // 
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

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

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

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var 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;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
  console.log('Listening on ' + bind);
}
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>

        <handlers>
            <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
        </handlers>\
<!-- Removed in an effort to get a valid URL response -->
        <!-- <rewrite>
            <rules>
                <rule name="api">
                    <match url="/*" />
                    <action type="Rewrite" url="/*:8000" />
                </rule>
            </rules>
        </rewrite> -->

        <security>
            <requestFiltering>
                <hiddenSegments>
                    <add segment="node_modules" />
                </hiddenSegments>
            </requestFiltering>
        </security>
    </system.webServer>
</configuration>

请让我知道解决此问题还需要哪些其他数据(如果有)。

*** 编辑 *** 通过更改 web.config 启用该部分并将所有请求指向 /app.js 取得了一些进展。 这是我现在收到的错误消息:

iisnode encountered an error when processing the request.

HRESULT: 0x2
HTTP status: 500
HTTP subStatus: 1002
HTTP reason: Internal Server Error
You are receiving this HTTP 200 response because system.webServer/iisnode/@devErrorsEnabled configuration setting is 'true'.

In addition to the log of stdout and stderr of the node.exe process, consider using debugging and ETW traces to further diagnose the problem.

The node.exe process has not written any information to stderr or iisnode was unable to capture this information. Frequent reason is that the iisnode module is unable to create a log file to capture stdout and stderr output from node.exe. Please check that the identity of the IIS application pool running the node.js application has read and write access permissions to the directory on the server where the node.js application is located. Alternatively you can disable logging by setting system.webServer/iisnode/@loggingEnabled element of web.config to 'false'.

它引用的 2 个链接都是 10 多年前写的,没有提供我能辨别的有用信息。

我还尝试将我的 app.js 代码包装在 try/catch 块中,并使用 fs 将错误日志写入文本文件,但没有成功。 我相信在抛出错误之前它甚至没有访问 app.js。 所有路径都被赋予了对“每个人”的全面“完全控制”,只是为了完全消除权限问题(目前)。

更新了 web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>

        <handlers accessPolicy="Execute">
            <add name="iisnode" path="app.js" verb="*" modules="iisnode" resourceType="Either" requireAccess="Execute" />
        </handlers>
        <rewrite>
            <rules>
                <rule name="api">
                    <match url="/*" />
                    <action type="Rewrite" url="/app.js/*" />
                </rule>
            </rules>
        </rewrite>

        <security>
            <requestFiltering>
                <hiddenSegments>
                    <add segment="node_modules" />
                </hiddenSegments>
            </requestFiltering>
        </security>
        <tracing>
            <traceFailedRequests>
                <add path="*">
                    <traceAreas>
                        <add provider="ISAPI Extension" verbosity="Verbose" />
                        <add provider="WWW Server" areas="Authentication,Security,Filter,StaticFile,CGI,Compression,Cache,RequestNotifications,Module,FastCGI,WebSocket,ANCM,Rewrite,RequestRouting,iisnode" verbosity="Verbose" />
                    </traceAreas>
                    <failureDefinitions statusCodes="403.14" verbosity="Warning" />
                </add>
            </traceFailedRequests>
        </tracing>
        <iisnode watchedFiles="*.js" loggingEnabled="true" logDirectory="iisnode" devErrorsEnabled="true" />
    </system.webServer>
</configuration>

我已经极大地修改了我的方法,现在在 IIS 上将应用程序作为 2 个站点运行。 后端在端口 90 上运行,前端 (REACT) 在端口 80 上运行,对端口 90 的请求硬编码在代码中。

现在正在确定为什么 cors() 无法正常运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM