【问题标题】:Node.js Express app only works via localhost, does not work over network (File or Directory not found)?Node.js Express 应用程序只能通过 localhost 工作,不能通过网络工作(找不到文件或目录)?
【发布时间】:2020-01-02 20:20:14
【问题描述】:

我正在尝试创建一个应该位于 IIS 服务器上并通过 Internet 访问的 node.js express 应用程序。但是,到目前为止,我只能通过 localhost 来完成这项工作。我的 node.js 应用程序在通过本地计算机上的 localhost 和 Windows Server 2012 R2 计算机上的 IIS 访问它时工作,但尝试通过 URL 或 IP 访问时返回以下页面:

我不确定到底发生了什么,但过去一天我一直在努力解决这个问题。如果有人对此有任何想法并能对此有所了解,将不胜感激。

这是我的 app.js(.env 文件不包含 PORT 或 IP 的变量,仅用于密钥)

var express = require('express');
var app = express();
var router = require('./routes/index.js');
var bodyParser = require('body-parser');
var dotenv = require('dotenv');

dotenv.config();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.use(router);

var port = process.env.PORT;
var ip = process.env.IP;

app.listen(process.env.PORT, process.env.IP, () => {
    console.log(`Server running on ${process.env.IP}:${process.env.PORT}`);
});

用于路由的 index.js

var express = require('express');
var router = express.Router();
var sfdcSystemController = require('../controllers/Salesforce/SystemController.js');
var jwt = require('jsonwebtoken');
var fs = require('fs');

router.get('/',sfdcSystemController.doGet);

module.exports = router;

iisnode 的 web.config

<configuration>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="app.js" verb="*" modules="iisnode" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="nodejs">
          <match url="api/*" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          </conditions>
          <action type="Rewrite" url="/app.js" />
          </rule>
      </rules>
    </rewrite> 
    <security>
      <requestFiltering>
        <hiddenSegments>
          <add segment="node_modules" />
          <add segment="iisnode" />
        </hiddenSegments>
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

【问题讨论】:

  • 这看起来更像是网络问题,请求永远不会到达你的 node.js 服务器。
  • 发现问题,其实是IIS问题。答案如下。

标签: node.js express networking localhost iisnode


【解决方案1】:

找出我的问题。事实证明这更多是 IIS 的问题,以及它在服务器上的配置方式与我的代码在做什么。

在 IIS 中,我必须执行以下操作才能让我的 node.js 应用程序正常工作...

  1. 更新匿名身份验证,确保它已启用,并授予我的 App Pool Identity 访问权限,而不是服务器用户。
  2. 将错误页面功能设置更新为“详细错误”
  3. 为我的新 node.js 应用程序提供自己的 IP,而不需要主机头。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 2013-02-05
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多