【问题标题】:Azure Node.js Internal Server Error from index.js file来自 index.js 文件的 Azure Node.js 内部服务器错误
【发布时间】:2018-09-27 20:18:31
【问题描述】:

我有一个在本地运行的 Node.js 应用程序,但是我花了很长时间试图通过无数教程和官方指南来弄清楚如何在 Azure 上部署它。但是,我不断收到以下错误 “该页面无法显示,因为发生了内部服务器错误。”,我认为这是由我的 index.js 文件引起的。我的应用只包含一个 index.js、package.json 和一个 main.html 文件。

你能不能看看我下面的 index.js 和 package.json 文件看看 如果你能发现任何错误?感谢您的帮助。

index.js

var express = require('express');
var app = express();


app.render('main', function(err, html) {
console.log(html)
});


var port = process.env.PORT || 1337;
server.listen(port);
console.log("Server running at http://localhost:%d", port);

package.json

{
"name": "myLocalProj",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"ejs": "^2.6.1",
"express": "^4.16.3",
"path": "^0.12.7"
}
}

再次感谢您。

【问题讨论】:

    标签: node.js azure express azure-web-app-service


    【解决方案1】:

    看起来你没有在问题中发布完整的代码 sn-p,所以我在访问 Azure Web 根 url 时使用了一个模板来显示 main.html。

    var express = require('express');
    var app = express();
    
    app.set('views', __dirname);
    app.set('view engine', 'html');
    app.engine('html', require('ejs').renderFile);
    
    // This method only prints main.html in console
    app.render('main', function(err, html) {
        console.log(html);
    });
    
    app.get('/', function (req, res) {
        res.render('main');
    });
    
    var port = process.env.PORT || 1337;
    app.listen(port);
    console.log("Server running at http://localhost:%d", port);
    

    要让这个节点应用在 Azure(IIS) 上运行,我们需要一个 web.config 文件。

    <?xml version="1.0" encoding="utf-8"?>
    <!--
         This configuration file is required if iisnode is used to run node processes behind
         IIS or IIS Express.  For more information, visit:
    
         https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
    -->
    
    <configuration>
      <system.webServer>
        <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
        <webSocket enabled="false" />
        <handlers>
          <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
          <add name="iisnode" path="index.js" verb="*" modules="iisnode"/>
        </handlers>
        <rewrite>
          <rules>
            <!-- Do not interfere with requests for node-inspector debugging -->
            <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
              <match url="^index.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 site entry point -->
            <rule name="DynamicContent">
              <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
              </conditions>
              <action type="Rewrite" url="index.js"/>
            </rule>
          </rules>
        </rewrite>
    
        <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
        <security>
          <requestFiltering>
            <hiddenSegments>
              <remove segment="bin"/>
            </hiddenSegments>
          </requestFiltering>
        </security>
    
        <!-- Make sure error responses are left untouched -->
        <httpErrors existingResponse="PassThrough" />
    
        <!--
          You can control how Node is hosted within IIS using the following options:
            * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
            * node_env: will be propagated to node as NODE_ENV environment variable
            * debuggingEnabled - controls whether the built-in debugger is enabled
    
          See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
        -->
        <!--<iisnode watchedFiles="web.config;*.js"/>-->
      </system.webServer>
    </configuration>
    

    所以文件夹结构应该是这样的。

    你不提你是怎么部署代码的,我只是用Zip Deploy来测试。

    创建一个包含所有内容(包括 node_modules)的 zip 文件,转到 https://yourwebappname.scm.azurewebsites.net/ZipDeploy 并将其拖到浏览器中。

    然后节点应用程序应该按预期工作。

    【讨论】:

    • 非常感谢您的帮助,这非常有效。诀窍是 web.config 文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多