首页 > 解决方案 > 来自 index.js 文件的 Azure Node.js 内部服务器错误

问题描述

我有一个在本地运行的 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);

包.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.jsazureexpressazure-web-app-service

解决方案


看起来您没有在问题中发布完整的代码片段,因此我在访问 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将其拖到浏览器中。

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


推荐阅读